How can I discover a mongo database's structure

I have a Mongo database that I did not create or architect, is there a good way to introspect the db or print out what the structure is to start to get a handle on what types of data are being stored, how the data types are nested, etc?


Solution 1:

Just query the database by running the following commands in the mongo shell:

use mydb //this switches to the database you want to query
show collections //this command will list all collections in the database
db.collectionName.find().pretty() //this will show all documents in the database in a readable format; do the same for each collection in the database

You should then be able to examine the document structure.

Solution 2:

There is actually a tool to help you out here called Variety:

http://blog.mongodb.org/post/21923016898/meet-variety-a-schema-analyzer-for-mongodb

You can view the Github repo for it here: https://github.com/variety/variety

I should probably warn you that:

  • It uses MR to accomplish its tasks
  • It uses certain other queries that could bring a production set-up to a near halt in terms of performance.

As such I recommend you run this on a development server or a hidden node of a replica or something.

Depending on the size and depth of your documents it may take a very long time to understand the rough structure of your database through this but it will eventually give one.

Solution 3:

This will print name and its type

var schematodo = db.collection_name.findOne()
for (var key in schematodo) { print (key, typeof key) ; }

Solution 4:

I would recommend limiting the result set rather than issuing an unrestricted find command.

use mydb
db.collectionName.find().limit(10)
var z = db.collectionName.find().limit(10)
Object.keys(z[0])
Object.keys(z[1])

This will help you being to understand your database structure or lack thereof.

Solution 5:

This is an open-source tool that I, along with my friend, have created - https://pypi.python.org/pypi/mongoschema/

It is a Python library with a pretty simple usage. You can try it out (even contribute).