Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I discover a mongo database's structure

Tags:

mongodb

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?

like image 743
tolmark Avatar asked Feb 05 '13 17:02

tolmark


People also ask

How do I find the database schema in MongoDB?

We can get the schema object/first document of the collection using : var schemaObj = db. users. findOne();

What is the structure of MongoDB db?

MongoDB is a document-oriented NoSQL database management system (DBMS). Unlike traditional relational DBMSs, which store data in tables consisting of rows and columns, MongoDB stores data in JSON-like structures referred to as documents.

How do I visualize a schema in MongoDB?

Deducing a Virtual SchemaDbSchema can connect to MongoDB, look in the sample collection records, and deduce a virtual diagram from there. All you have to do is to connect to MongoDB and the tool will deduce the schema automatically. Once the diagram is deduced, the possibilities are endless.


2 Answers

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.

like image 105
br3w5 Avatar answered Sep 28 '22 12:09

br3w5


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.

like image 20
Sammaye Avatar answered Sep 28 '22 12:09

Sammaye