How can I discover a mongo database's structure

Mongodb

Mongodb Problem Overview


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?

Mongodb Solutions


Solution 1 - Mongodb

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 - Mongodb

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 - Mongodb

This will print name and its type

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

Solution 4 - Mongodb

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 - Mongodb

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).

Solution 6 - Mongodb

One option is to use the Mongoeye. It is open-source tool similar to the Variety.

The difference is that Mongoeye is a stand-alone program (Mongo Shell is not required) and has more features (histograms, most frequent values, etc.).

https://github.com/mongoeye/mongoeye

Solution 7 - Mongodb

Few days ago I found GUI client MongoDB Compass with some nice visualizations. See the product overview. It comes directly from the mongodb people and according to their doc:

> MongoDB Compass is designed to allow users to easily analyze and understand the contents of their data collections within MongoDB...

Solution 8 - Mongodb

You may've asked about validation schema. Here's the answer how to get it: https://stackoverflow.com/questions/45285852/how-to-retrieve-mongodb-collection-validator-rules

Solution 9 - Mongodb

Use Mongo Compass

which does a sample as explained here Which does a random sample of 1000 documents to get you the schema - it could miss something but it's the only rational option if you database is several GBs.

Visualisation

Mongo Compass Schema tab

The schema then can be exported as JSON

enter image description here

Documentation

Solution 10 - Mongodb

You can use MongoDB's tool mongodump. On running it, a dump folder is created in the directory from which you executed mongodump. In that folder, there are multiple folders that correspond to the databases in MongDB, and there are subfolders that correspond to the collections, and files that correspond to the documents.

This method is the best I know of, as you can also make out the schema of empty collections.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontolmarkView Question on Stackoverflow
Solution 1 - Mongodbbr3w5View Answer on Stackoverflow
Solution 2 - MongodbSammayeView Answer on Stackoverflow
Solution 3 - MongodbprathameshView Answer on Stackoverflow
Solution 4 - MongodbPaulView Answer on Stackoverflow
Solution 5 - MongodbUtsav TView Answer on Stackoverflow
Solution 6 - MongodbMichal JurečkoView Answer on Stackoverflow
Solution 7 - MongodbIkar PohorskýView Answer on Stackoverflow
Solution 8 - MongodbLeammasView Answer on Stackoverflow
Solution 9 - MongodbMarkView Answer on Stackoverflow
Solution 10 - Mongodbuser3980196View Answer on Stackoverflow