MongoDB
DbSchema connects to MongoDB and draws your collections as a diagram, with their fields, nested documents and arrays. MongoDB does not require a fixed structure, so DbSchema finds the structure itself. Some collections have a validator: a set of rules that MongoDB checks when it saves a document. For a collection with a validator, DbSchema reads the fields from the validator. For a collection without one, it reads a sample of the documents. From the diagram, you link related collections, browse documents and run queries.
If you know SQL databases, these MongoDB terms are close to the ones that you know:
| SQL database | MongoDB |
|---|---|
| Table | Collection |
| Row | Document |
| Column | Field |
| Primary key | _id, the field that identifies a document in its collection |
| Foreign key | A field that holds the _id of a document in another collection. DbSchema draws it as a virtual relation. |
DbSchema keeps the diagram and its relations in your design file.
The design is DbSchema's own copy of the structure. Most changes to the design stay in DbSchema.
For each task, this page explains whether it also changes MongoDB.
The pictures on this page use a small MongoDB database, tour_shop, with the collections customers, orders, products, reviews and coupons.
Connect to MongoDB
- Choose File → New Model, or Start New on the Welcome screen.
- Keep Connect to Database selected, click MongoDb, and press Connect to MongoDb.
- Choose the Method, such as Cloud (Atlas) - Password Authentication for MongoDB Atlas, a MongoDB cloud service. For a server on your own computer without a password, choose On-Premise - No Authentication.
- Under Where is the database running?, choose This computer, default port. For a server on another computer, or on another port, choose Remote computer or custom port. Then type
host:port: the name of the computer and the port number of the server. - Type or choose the Database. If the method asks for a user and a password, type them too.
- Press Connect.
- In Reverse Engineer the Databases, tick the databases to read and press Load. The filter field at the top finds a database by its name.
DbSchema reads the collections and draws them on a diagram. Only the structure is copied into your design, not the documents. 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 lists every field that it finds, with its type.
- A shield at the right of the collection name marks a collection with a validator. DbSchema shows the fields of the validator.
- A crossed-out shield marks a collection without a validator. DbSchema finds its fields by reading a sample of documents. Fields that do not appear in the sample are not shown on the diagram.
- A nested document is a document inside another document. For example,
addresshas the typeobject. Its fields appear below it. - An array of documents is a list of documents stored in one field. For example,
itemshas the typearray[object]. The fields of its documents appear below the array.
To show or hide the fields of a nested document or array, click the small arrow next to its name on the diagram.
To see a collection in detail, double-click its name on the diagram. The Edit Collection dialog opens.
If the collection has no validator, Inferred ( no validation ) is ticked.
For orders, which has a validator, it is not ticked.
The Details column shows the rules for each field.
For example, it shows not null for a field that the validator requires, or the allowed values of status.
Read more documents to find every field
If a collection has no validator, DbSchema reads the first 100 and the last 100 documents.
This depth is called fast, and it is the default.
A field that appears only in other documents is missing from your design.
To read more documents:
- Choose Database → Manage Connections.
- Press the pencil button, Edit connection, on the row of your connection.
- Open the Network and Driver 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. Choose
medium, which reads 300 documents from each end, orfull, which reads every document. - Press OK in each dialog, then choose Database → Refresh Model from Database.
Schema Scan Depth applies to all collections that DbSchema reads using this connection. A deeper scan takes longer on a large collection.
See the relations between collections
MongoDB does not declare relations between collections.
A field such as reviews.customer_id stores the _id of a customer. MongoDB does not recognize this as a relation.
DbSchema keeps such a link as a relation, and draws it as a line between the collections.
These are called virtual relations: DbSchema stores them only in your design file, not in MongoDB.
When DbSchema reads the documents of a collection, it can find some relations by itself.
In tour_shop, it found the relations of reviews to customers and to products.
It found no relations for orders. DbSchema reads the fields of this collection from its validator.
The Relationships tab of the Edit Collection dialog lists the relations of a collection.
Add a relation between collections
Add the relations that DbSchema did not find. The Data Explorer and the Query Builder use them to show related documents.
- On the diagram, drag a field such as
orders.customer_idonto the field that it refers to, such ascustomers._id. - The New Relationship dialog opens with Virtual ticked. It names the relation
fk_orders_customers. Press OK.
The line from orders to customers shows on the diagram.
The relation changes your design only. Save the design to keep it.
Model → Find Virtual Relations looks for more relations by matching field names.
Browse documents in the Data Explorer
The Data Explorer shows documents from one collection alongside related documents from another collection. The Data Explorer needs DbSchema Pro.
- Right-click a collection on the diagram, such as
customers, and choose Data Explorer. - The documents show one at a time. The arrows at the bottom move to the next or previous document.
- To narrow down the documents, type a JSON filter in the field Type a JSON filter or right-click a column. A JSON filter is a condition written in JSON, the text format of MongoDB documents, such as
{"address.city": "Berlin"}for the customers in Berlin. - Click the arrow button next to the collection name and choose Cascade orders via fk_orders_customers.
The orders of the selected customer show on the right, joined to the customer by a line. When you select another customer, the Data Explorer shows the orders of that customer. The Data Explorer opens under the diagram. Double-click its tab to expand the Data Explorer to fill the area. 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, the language of MongoDB's own command-line shell. Start it with the database name, such as
tour_shop.orders. - Press Run.
tour_shop.orders.find({ status: "shipped" }, { ordered_at: 1, status: 1, total: 1, items: 1 })
The first part, { status: "shipped" }, selects the shipped orders.
The second part lists the fields to show, each with 1.
The result shows as a table. The panel on the right shows the selected document as a Tree or as JSON. An aggregation calculates results from the documents. This example counts how many orders have each status:
tour_shop.orders.aggregate([{ $group: { _id: "$status", orders: { $sum: 1 } } }])
The language button also offers Java Groovy and JavaScript, for scripts. To build a query with the mouse instead, choose Query → Query Builder, or press Ctrl+B. The Query Builder needs DbSchema Pro.
Compare your design with MongoDB
Your design and the database can become different. For example, you change the design while offline, or someone changes a validator in MongoDB. DbSchema compares the design with the database. For each difference, you choose which one to update. Schema synchronization needs DbSchema Pro.
- Start the comparison in one of two ways:
- If you work offline, without a connection, open the Database menu and choose your connection by its name. In the Connected to the Database dialog, choose Compare, then decide difference by difference.
- If you are already connected, press Refresh in the toolbar.
- In Synchronize with the Databases, keep the database ticked and press Compare.
- Choose Review Differences.
Each row is one difference. In this example, the field birthday was added to customers in the design while offline.
The word absent shows where the item is missing: in the design or in the database.
For each row, choose whether to update the design or the database, or leave both unchanged:
- The arrow to the left, such as Drop here, changes the design to match the database.
- The arrow to the right, such as Create here, changes the database to match the design.
- Ignore leaves both sides unchanged.
Then press Apply to the model or Commit in the database. Close leaves both sides unchanged. Schema Synchronization explains this dialog in more detail.
Create collections and validators
While you are connected, pressing OK in Edit Collection applies the change to both your design and MongoDB. Collection Validator, at the bottom of the dialog, shows the commands that DbSchema will run.
DbSchema can create collections and their validators in MongoDB from your design.
- Model → Create Collection adds a collection.
- In Edit Collection, the Fields tab sets the type of each field, and whether the field is required (Mandatory ( Not Null )).
- In the Validator Level & Action tab, choose when MongoDB checks the rules (
validationLevel). Choose also whether MongoDB rejects a document that breaks the rules, or only gives a warning (validationAction). - If you want a collection to have no validator, tick Inferred ( no validation ).
- 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.
Common problems
A field is missing from a collection
For a collection without a validator, DbSchema reads only part of the documents. Raise the Schema Scan Depth, as Read more documents shows, and refresh the model.
A relation between two collections is missing
DbSchema does not find every relation by itself.
Drag the field onto the _id that it refers to, as Add a relation shows, or use Model → Find Virtual Relations.