Article

Diagrams and Data Explorer for MongoDB

MongoDB stores information based on JSON documents. The complexity of the documents increase if the number of sub-documents increase. Mastering such a documents using diagrams may make work much easier. DbSchema (www.dbschema.com) can represent the documents using diagrams. Let's consider this JSON document :

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",                                               _
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}

This will be represented in DbSchema as three entities, one for each sub-document.

Diagram for MongoDB

You may ask how is this possible, since MongoDB use no schema as relational databases does. Well, DbSchema does discover the schema by scanning the data. What to have to do is to start DbSchema, connect to the database ( provide the host name where the database is running ) and the job is done.

Inside DbSchema you may create further diagrams, with the entities you want. Diagrams are called 'layouts', because in each layout you can open different other tools, like Visual Query Builder, Query Editor or Relational Data Browse.

If your database is empty and you wish to create the collection above in the database, open a Query Editor inside DbSchema and execute the code below:


local.persons <<
[
  firstName: "John",
  lastName: "Smith",
  isAlive: true,
  age: 25,
  address: [
    streetAddress : "21 2nd Street",
    city : "New York",
    state : "NY",
    postalCode : "10021-3100"
  ],
  phoneNumbers: [
    [
      type: "home",
      number: "212 555-1234"
    ],
    [
      type: "office",
      number: "646 555-4567"
    ]
  ],
  children: [],
  spouse: null
]

DbSchema can execute JSON commands via Java Groovy, which is very similar with the MongoDB native language. The major difference is that arrays as [val1,val2,...] and maps are [key1:val1,key2:val2,...]. Read more about the possible commands on MongoDB page.

When leaving the application you can save the project including the diagrams to a project file. Next time when you start the application all diagrams will be as they were before.

Relational Data Browse

Relational Data Browse is a tool inside DbSchema for exploring data from multiple collections. The nice stuff about it is the fact that it can explore data from collections which are referring data from other collections.

To show this consider the two collections below. We see that the 'employee' collection is referring 'companies' via 'companyId'. This is a normalization of the information inside the database, so the information related to a company is stored only one time, in the companies collection and it is referred from the employees.

                        companies :
                        {
                           name : "Wise Coders",
                           address : "Wildstreet 12, Montdown 22334"
                        }

                        employees :
                        {
                           firstName : "John",
                           lastName : "Cary"
                           companyId : new ObjectID('559b957f4451301bdc0cae45')
                        }
                        

In the image below we have the two collections employees and companies. We have create a virtual foreign key from employees to companies by drag and drop of the companyId over the _id fields. The virtual foreign key helped us to explore the data from the employees according to the currently selected data in companies.

Relational data browse for MongoDB

This foreign key will exists only in DbSchema. Using it we will be able to browse data from both collections and see for each employee the company data, without having to execute each time a separate query. The random data generator can be started from the right-click popup on any collection in the diagram, then open the second frame by pressing the foreign key arrow icon from the title of the first browse frame.

Notice that is no need to create the virtual foreign key each time we access the application. DbSchema saves the layouts ( diagrams ) and the schema into a project file which will be opened next time when accessing the application.

Query Editor

Beside diagrams and relational data browse you can make use of the query builder and data loader for MongoDB. Using the Query Editor you can edit and execute queries based on Groovy language, a Java - compatible scripting language. The result is displayed as a nested table structure.

Query Editor for MongoDB

DbSchema is supporting most of the relational databases plus Mongo DB. The tool is commercial with a trial period of two weeks.

Navigation