Schema Modeling

The schema modeling process requires some conceptual basics:

Let's say you want to create a ToDo app and you need to save todo objects represented by JSON objects. At first, you need to define a SCHEMA that models your todo objects. A todo-object is an instances of your SCHEMA and is saved in a COLLECTION named todo, along with other todo-objects. All your COLLECTIONS are persisted in your active MongoDB DATABASE.

  • Database: The data store that is used by your project
  • Collection: similar objects are persisted in a collection
  • Schema: A blueprint for the objects of a collection
  • Object: JSON object that matches a schema of a collection

Create new collection

In your project's admin dashboard, go to Database > Collections and create a new collection. You will be prompted to enter a collection name. Choose a meaningful collection name. You will not be able to change existing collection names. Most important, we will generate a unique collection ID by lowercasing your collection name, ignoring all special characters except '-' (Minus). We choose the collection name ToDo and get the unique collection identifier todo.

Model the schema

In your project's admin dashboard, go to Database > Collections and click on your collection's submenu entry Schema. This will open a schema modeler where you can model a JSON blueprint for your collection. For our ToDo example, we may have JSON objects like this one:

{
    "name": "Cocktails at 9pm",
    "details": "Meet with Matt at Enchilada to celebrate his birthday",
    "startTimestamp": 1483477200,
    "endTimestamp": 1483486200,
    "userId": "u12345",
    "tags": ["party", "Matt", "Saturday"],
    "customData": {
        "location": "Enchiladas",
        "notificationActive": true
    }
}

If you want to model the schema for this kind of objects, you have to define the KEYS (left side of the assignments) like details or startTimestamp and the TYPE of the final value (right side of the assignments). Additional settings can be set in FIELD SETTINGS. Add a new field by clicking on NEW PROPERTY or by dragging & dropping one of the available TYPES from the Toolbox to your schema. Then you have to enter a valid FIELD NAME (uppercase/lowercase letters, digits, underscores, minus). The available types are:

  • String
  • Number
  • Date (Unix timestamp)
  • Boolean
  • Nested (Key that holds another object that has its own keys and values)
  • Object (Reference to an object in another collection of your database)
  • [String] (Array of Strings)
  • [Number] (Array of Numbers)
  • [Date] (Array of Dates)
  • [Boolean] (Array of Boolean values)
  • [Nested] (Key that holds an array of objects)

Using our example object from above, we model our ToDo schema like this:

{
    "name": String,
    "details": String,
    "startTimestamp": Date,
    "endTimestamp": Date,
    "userId": String,
    "tags": [String],
    "customData": Nested
        {
            "location": String,
            "notificationActive": Boolean
        }
}

After saving your schema, you can access the collection by its collectionId using the REST-API.

Object Metadata

To simplify data handling, we always add a unique _id, timestamps and versioning to your collection objects. If you create a new object, we will append the following information:

  • _id: (String) Unique object identifier
  • __version: (Number) Version of this object. Increased with every PUT update.
  • __createdAt: (Date) Insert timestamp
  • __updatedAt: (Date) Last update timestamp
  • __createdBy: (String) UserId of the user that created this object

If we create the example todo object from above, the persisted object will look like this:

{
    "_id": "21l3k4j23l4j",
    "name": "Cocktails at 9pm",
    "details": "Meet with Matt at Enchilada to celebrate his birthday",
    "startTimestamp": 1483477200,
    "endTimestamp": 1483486200,
    "userId": "u12345",
    "tags": ["party", "Matt", "Saturday"],
    "customData": {
        "location": "Enchiladas",
        "notificationActive": true
    }
    "__version": 0,
    "__createdAt": 1483477200,
    "__updatedAt": 1483477200,
    "__createdBy": "u11111"
}