MongoDB schema design

The following text explains how to design a new database structure for MongoDB.
The MongoDB schema design article is divided into the following sections:

  1. New project
  2. MongoDB schema design - collections
  3. MongoDB schema design - documents
  4. Embedded documents vs References
  5. Code generation

1. New Project

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

2. Design collections

Start adding new objects to your diagram, you can add the following MongoDB specific items to your design: Collections, Documents, References, Views, Functions and Other objects. To add new MongoDB collection to your design, click the Collection item on toolbar and then click the main diagram area.

MongoDb schema design - new collection

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

Edit properties

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

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

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 required field (not null)

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

New field

Field specifics

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

MongoDB schema design - field details

3. Design documents

MongoDB documents are objects that can be embedded to collections or other documents.

Example: See collection customer. Field contact contains embedded document contact. And this document contains two fields: addresses and phones. Both the fields are arrays and contain embedded documents address and phone.

MongoDB chema design

To create new document, click the Document icon on toolbar and then click the main area.

Embedding documents to collections

To embed the document to a collection, edit the target collection and select document name from Datatype selectbox.

Embedded document

Collections vs Documents in MongoDB design process

During your design process you might consider changing document to collection or vice versa. In order to avoid manual redefinition, enable or disable the Document switch in properties. This switch allows you to change document to collection and let the tool generate MongoDB validation script for the collection.

Collection vs document

4. References in MongoDB schema design

References represent links between collections. In the following example, collection article contains reference to collection vendor. In the article collection only ID is stored. Compare it with the vendor collection where field address contains embedded document address. Such field contains full data, incl. street, city, zip and country values.

References

5. Code generation

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

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

Validation scripts

This is the end of the MongoDB schema design page. Thanks for reading!

Copyright: 2019 Datensen. All rights reserved.
This website is not endorsed by MongoDB, Inc.
This website uses cookies to ensure you get the best experience on our website.