MongoDB
DbSchema connects to MongoDB and reads the structure of your collections from their documents. It draws the collections as a diagram, with their nested documents, arrays, and the relations between collections. From the diagram you browse documents and run queries.
The pictures on this page use a small MongoDB database shop, with the collections customers, orders and products.
Connect to MongoDB
- Choose File → New Model.
- Keep Connect to Database selected, click MongoDb, and press Connect.
- Choose the Method, such as Cloud (Atlas) - Password Authentication for MongoDB Atlas or On-Premise - No Authentication for a local server.
- Under Where is the database running?, choose This computer, default port or Remote computer or custom port. For a custom port, type
host:port, such as127.0.0.1:57017. To use a connection string you already have, such as one from Atlas, choose Paste a JDBC URL as the Method instead, and paste it. - Type or choose the Database, and the user and password if the method asks for them.
- Press Connect.
- In Reverse Engineer the Databases, tick the databases to read and press Import.
DbSchema reads the collections and draws them on a diagram. Connect to a database explains the connection dialog in more detail.
See the fields of a collection
A MongoDB collection has no fixed columns: each document can have its own fields. DbSchema reads documents of each collection and lists every field it finds, with its type.
- A nested document, such as
address, shows as a field of typeobjectwith its own fields under it. - An array of documents, such as
items, shows asarray[object], with the fields of its documents under it. - An array of values, such as
tags, shows as an array.
Click the small arrow next to a nested field on the diagram to show or hide its fields.
To see a collection in detail, double-click its header. The Edit Collection dialog opens.
Inferred ( no validation ) is ticked for a collection that has no validator in MongoDB: DbSchema found its fields by reading documents.
For a collection with a $jsonSchema validator, DbSchema reads the fields from the validator instead.
Read more documents to find every field
DbSchema reads 100 documents from the start and 100 from the end of each collection. A field that appears only in other documents can be missing from the model.
- Choose Database → Manage → Edit Active Connection.
- Open the Advanced tab and press the pencil next to SSL & Parameters.
- Choose DbSchema Schema Discovery in the tree.
- Turn on the switch next to Schema Scan Depth and choose
medium, which reads 300 documents from each end, orfull, which reads every document. - Press OK, then choose Database → Refresh Model from Database.
A deeper scan takes longer on large collections.
See the relations between collections
MongoDB does not declare relations between collections.
DbSchema finds them by value: where a field holds the _id of a document in another collection, DbSchema draws a line.
In the shop database, orders.customer_id points to customers and items.product_id points to products.
These relations are virtual: DbSchema keeps them in the model file, and MongoDB never sees them. The Relationships tab of the Edit Collection dialog lists them.
To add a relation that DbSchema did not find, drag the field onto the field it refers to. Model → Find Virtual Relations looks for more relations by matching field names.
Browse documents in the Data Explorer
The Data Explorer shows the documents of a collection, and the documents of related collections next to them.
- Right-click a collection header and choose Data Explorer.
- The documents show one at a time, with arrows to move between them. Type a JSON filter in Type a JSON filter or right-click a column to narrow them down.
- Click the arrow icon next to the collection name and choose Cascade orders via Relationship.
The orders of the selected customer show on the right, joined to the customer by a line. Relational Data Explorer explains the Data Explorer in more detail.
Run MongoDB queries
- Choose Query → Query Editor, or press Ctrl+E. The language button of the editor reads Mongo Queries.
- Type a query in MongoDB shell syntax. Start it with the database name, such as
shop.orders. - Press Run.
shop.orders.find({ status: 'shipped' }, { order_date: 1, status: 1, items: 1 })
The result shows as a table. The panel on the right shows the selected document as a Tree or as JSON. Aggregations work the same way:
shop.orders.aggregate([{ $group: { _id: '$status', orders: { $sum: 1 } } }])
The Query menu has a second Query Editor, Ctrl+B, which opens the visual query builder.
Create collections and validators
A MongoDB validator is a $jsonSchema rule that a document must follow to be saved in a collection.
DbSchema can create collections and their validators from the model.
- Model → Create Collection adds a collection to the model.
- In Edit Collection, tick Inferred ( no validation ) to keep the collection in the DbSchema model only, with no validator.
- Database → Create or Upgrade the Collection Validators into the Database writes the collections and their validators to MongoDB.
- File → Export As... → Export Schema Validation Script saves the same commands as a script.
While you are connected, a change that you confirm in Edit Collection reaches the database too. Collection Validator at the bottom of the dialog shows the commands that DbSchema will run.
Common problems
A field is missing from a collection
DbSchema reads only part of each collection. Raise the Schema Scan Depth, as Read more documents shows, and refresh the model.
A relation between two collections is missing
DbSchema finds a relation only where a field holds an ObjectId that matches the _id of a document in the other collection.
A reference stored as a string or a number gets no line.
Drag the field onto the field it refers to, or use Model → Find Virtual Relations.