Physical data modeling for MongoDB

Data stored in MongoDB has a very flexible schema. When we do the physical data modeling for MongoDB, instead of defining the structure of schemas, we write validators to enforce the rules on the fields that we have in our data model. In this way, we can make sure that each document in the collection will have those required fields we specified in the validator and they will be in the data type we require. And, as a naming convention of MongoDB, all of the collection's names will be plural, for example, User entities will be stored in collection users.

Let's look at an example; the following is a MongoDB script (for MongoDB Version 3.6 and later) that is used to create the teams collection. As you can see, most of this script is defining validation rules:

db.createCollection( "teams",{
"storageEngine": {"wiredTiger": {}},
"capped": false,
"validator": {
"$jsonSchema": {
"bsonType": "object",
"additionalProperties": false,
"properties": {
"_id": {"bsonType": "objectId"},
"name": {"bsonType": "string"},
"user_id": {"bsonType": "objectId"},
"archived": {"bsonType": "bool"},
"created_date": {"bsonType": "timestamp"}
},
"required": [
"_id",
"name",
"user_id",
"archived",
"created_date"
]
}
},
"validationLevel": "strict",
"validationAction": "error"
});

Due to the scope of this book, we will not go through this script. You can find all of the MongoDB scripts for creating all of the entities defined in the logical data model in the source code of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.27.251