How To: List keys of collection MongoDB

There are many things needed in the DBA’s life for which there are no simple and straight forward ways to find out and they sometimes are tricky to find out. One of them is to find the keys of a collection.

There are alternate methods we can certainly explore to retrieve all keys. the catch here is this method may miss some keys if it is present in somewhere other location in the documents.

Lets explore the method first, and will discuss how can we make sure that we get correct keys list.

We are using subreddits data sets for this purpose..

Lets switch to DB subreddits

MongoDB > use subreddits
 switched to db subreddits

I have created collections with same name subreddits for this purpose. With this collection we can use findOne() function which will return all data of the first document it found. The output would be as follows:

docs=db.subreddits.findOne()
{
“_id” : ObjectId(“5c2ef76aec3fae481c6aa8c4”),
“accounts_active” : null,
“accounts_active_is_fuzzed” : false,
“active_user_count” : null,
“advertiser_category” : null,
“allow_discovery” : true,
“allow_images” : true,
“allow_videogifs” : true,
“allow_videos” : true,
“audience_target” : “”,
“banner_img” : “”,
“banner_size” : null,
“can_assign_link_flair” : false,
“can_assign_user_flair” : false,
“collapse_deleted_comments” : false,
“comment_score_hide_mins” : 0,
“community_icon” : “”,
“created” : 1137566705,
“created_utc” : 1137537905,
“description” : “This subreddit is archived and no longer accepting submissions.”,
“description_html” : “<!– SC_OFF –><div class=\”md\”><p>This subreddit is <a href=\”https://redditblog.com/2011/10/18/saying-goodbye-to-an-old-friend-and-revising-the-default-subreddits/\”>archived and no longer accepting submissions.</a></p>\n</div><!– SC_ON –>”,
“display_name” : “reddit.com”,
“display_name_prefixed” : “r/reddit.com”,
“emojis_enabled” : false,
“header_img” : null,
“header_size” : null,
“header_title” : null,
“hide_ads” : false,
“icon_img” : null,
“icon_size” : null,
“id” : “6”,
“is_enrolled_in_new_modmail” : null,
“key_color” : “”,
“lang” : “en”,
“link_flair_enabled” : false,
“link_flair_position” : “”,
“name” : “t5_6”,
“notification_level” : null,
“over18” : false,
“primary_color” : “”,
“public_description” : “the original subreddit”,
“public_description_html” : “<!– SC_OFF –><div class=\”md\”><p>the original subreddit</p>\n</div><!– SC_ON –>”,
“public_traffic” : false,
“quarantine” : false,
“show_media” : false,
“show_media_preview” : true,
“spoilers_enabled” : true,
“submission_type” : “any”,
“submit_link_label” : null,
“submit_text” : “”,
“submit_text_html” : null,
“submit_text_label” : null,
“subreddit_type” : “archived”,
“subscribers” : 771116,
“suggested_comment_sort” : null,
“title” : “reddit.com”,
“url” : “/r/reddit.com/”,
“user_can_flair_in_sr” : null,
“user_flair_background_color” : null,
“user_flair_css_class” : null,
“user_flair_enabled_in_sr” : false,
“user_flair_position” : “right”,
“user_flair_richtext” : [ ],
“user_flair_template_id” : null,
“user_flair_text” : null,
“user_flair_text_color” : null,
“user_flair_type” : “text”,
“user_has_favorited” : false,
“user_is_banned” : false,
“user_is_contributor” : false,
“user_is_moderator” : false,
“user_is_muted” : false,
“user_is_subscriber” : false,
“user_sr_flair_enabled” : null,
“user_sr_theme_enabled” : false,
“videostream_links_count” : 0,
“whitelist_status” : “all_ads”,
“wiki_enabled” : true,
“wls” : 6
}

We need to iterate over the key-value pair in this document and separate out key from value list all keys. To do this use for loop to iterate over key-value pair and use print function to print all keys found.

MongoDB > for (key in docs) print(key);
_id
accounts_active
accounts_active_is_fuzzed
active_user_count
advertiser_category
allow_discovery
allow_images
allow_videogifs
allow_videos
audience_target
banner_img
banner_size
can_assign_link_flair
can_assign_user_flair
collapse_deleted_comments
comment_score_hide_mins
community_icon
created
created_utc
description
description_html
display_name
display_name_prefixed
emojis_enabled
header_img
header_size
header_title
hide_ads
icon_img
icon_size
id
is_enrolled_in_new_modmail
key_color
lang
link_flair_enabled
link_flair_position
name
notification_level
over18
primary_color
public_description
public_description_html
public_traffic
quarantine
show_media
show_media_preview
spoilers_enabled
submission_type
submit_link_label
submit_text
submit_text_html
submit_text_label
subreddit_type
subscribers
suggested_comment_sort
title
url
user_can_flair_in_sr
user_flair_background_color
user_flair_css_class
user_flair_enabled_in_sr
user_flair_position
user_flair_richtext
user_flair_template_id
user_flair_text
user_flair_text_color
user_flair_type
user_has_favorited
user_is_banned
user_is_contributor
user_is_moderator
user_is_muted
user_is_subscriber
user_sr_flair_enabled
user_sr_theme_enabled
videostream_links_count
whitelist_status
wiki_enabled
wls
MongoDB Enterprise >

The issue with this, if any document other than the one which we just iterated contains new key-value or does not have any existing key-value pair then it will impact the list we printed in the loop.

A solution to the issue is to have insight of data in collection where you might have new/removed keys using find() with proper document matching conditions and then iterate to print keys.

Leave a Reply

Your email address will not be published. Required fields are marked *