Skip to content

Instantly share code, notes, and snippets.



Last active Dec 13, 2015
What would you like to do?
Utility code to get the keys from a document in a specified MongoDB database and collection.
import sys
from pymongo import MongoClient
if len(sys.argv) < 3:
print 'usage is: ' + sys.argv[0] + ' databaseName collectionName'
dbName = sys.argv[1]
collectionName = sys.argv[2]
print 'Database: ' + dbName + ' Collection: ' + collectionName
connection_string = "mongodb://localhost"
connection = MongoClient(connection_string, safe=True)
db = connection[dbName]
collection = db[collectionName]
document = collection.find_one()
if document is not None:
print 'document keys: '
print document.keys()
print '*** No Document Found ***'

This comment has been minimized.

Copy link
Owner Author

@k0emt k0emt commented Feb 19, 2013

Note that this only gets the top level keys -- it doesn't delve into subdocuments.
On the command line give the utility the name of the database and the collection name you want examined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.