MongoDB schema design

Quick start guide for MongoDB schema design in Moon Modeler – a data modeling tool for MongoDB and noSQL databases.

How to get started with MongoDB schema design?

The following text explains how to design a new database structure for MongoDB.

1. Create a new Project

To create a new project in Moon Modeler click the New item on the toolbar. A new modal form opens. Select MongoDB project type and click Create a new project. A new project is created and you can start modeling your database structure.

2. Design collections

Start by adding collections to your project. To add a new MongoDB collection to your design, click the Collection item on the toolbar and then click the main diagram area.

toolbar - add a collection

A new graphical representative appears in the diagram. You can edit MongoDB collection properties via the Right Side panel, where all necessary settings can be easily specified. There is no need to confirm changes, all modifications are stored to the project automatically. In case you make a mistake, use UNDO to revert the changes back to the previous state.

collection details - side panel

If you prefer modal dialogs, double click an item on the diagram or open popup and choose Edit.

A modal form opens and you can navigate to an appropriate section by clicking a particular tab on the form. One of the advantages of using modal forms is the possibility to use the larger area for e.g. fields definition, for the specification of before and after scripts sections and so on.

MongoDB collection properties

In section Details, collection settings can be specified. For example caped collection, validation level, etc.

collection

Fields

In section Fields, you can add new fields to collections to your MongoDB schema design. Specify new field name, datatype and click Add.

The following abbreviations are used on the form:

  • ARR – indicates Array.
  • NN – represents the required field (not null)

Note: Both the settings are also displayed in the graphical representatives in your diagrams.

adding fields

Field specifics

To access areas where MongoDB field specifics can be modified, click the arrow icon left to the field name. The additional area expands and you can specify validation settings, description and sample data.

MongoDB schema design - field details

3. Design hierarchical structures

To create a hierarchical structure, use the Object data type (1). Moon Modeler will automatically create a new contained object, add a separate box to the diagram and draw a containment line pointing to the contained object box (2). Select the box and define new fields on the Fields tab (3).

You can also right click the object and select Edit from the context menu. A modal form opens where you can define properties and fields comfortably.

Modal form for a contained object in Moon Modeler diagram

Note: Because the contained object cannot exist independently, Moon Modeler automatically deletes the contained object when you delete the container (field, parent object or collection).

4. Draw references

References can be created between collections or between existing fields in collections/objects. In the following example you can see a reference created between the order and customer collections. In the order collection, only objectId value is stored.

Reference in MongoDB

To create a reference between existing fields, click the Reference button on the main toolbar, and then click one field in the source, and then click the other field in the target.

Note: in Moon Modeler diagrams can manage three different types of lines. References, lines and containment lines.

Tip: Lear more about noSQL data modeling

5. Generate validation scripts from your MongoDB schema design

To preview the validation script for your MongoDB schema design, click the Script tab on the collection detail. Moon Modeler can also generate scripts for the project. To see the full script click the Script item on the main toolbar. A modal form opens and the script is displayed with formatting and syntax highlighting.

To save scripts to files, click Save script, define a location where the output should be stored and finish the action.

MongoDB schema validation or MongoDB schema modification script generated by Moon Modeler.

6. Create ER diagrams of existing MongoDB databases

In Moon Modeler you can create a connection to the MongoDB database and use reverse engineering to create a project with an ER diagram. For more information, see Visualization of an existing database.

DATA MODELING TOOLS
DRAW Diagrams and
GENERATE Scripts
Download data modeling tools
Blog Categories
Blog by Platforms