Data schema design
The data storage service is a cloud-hosted database based on MongoDB that stores data in JSON format. As a developer, you can manipulate data directly from a client or use cloud functions to read and write data.
Database structure
The EMAS Serverless service uses MongoDB to store data in JSON format. Each record in the database is a JSON object. A database can contain multiple collections, which are similar to tables in a relational database. Each collection can be considered a JSON array.
The following table compares the EMAS Serverless MongoDB database with a MySQL relational database.
| Distributed file storage database (MongoDB) | Relational database (MySQL) |
| database | database |
| collection | table |
| document | row |
| field | column |
| index | index |
Automatically uses the _id field as the primary key | primary key |
Data schema design strategies
MongoDB is a NoSQL database that uses distributed file storage. It provides a scalable, high-performance data storage solution for web applications. Because its data model is flexible, you should design your schema based on the relationships between data.
Consider an E-commerce platform. The core data of an E-commerce platform typically includes products, users, shopping carts, and orders. Orders are generated when users purchase products. An order represents the relationship between a product and a user. Before an order is generated, a shopping cart maintains this same relationship.
In this scenario, you can design a product collection named products to store the following information:
- Basic product information: This includes display information and product specifications.
- Property information: Properties are associated with a product in a many-to-one relationship. It is best to store them as sub-documents in the
productscollection. - Inventory and price information: Inventory is associated not only with the product but also directly with product properties. Therefore, you should also store this information in the
productscollection.
The final database design is as follows:
{
"id": 5573,
"name": "Egg T-shirt",
"desc": {
"short": "Limited edition Egg T-shirt. Wear it to embrace your inner geek.",
"long": "This is a very long description.",
"category": {
"_id": "48bf43a..29e90bc",
"name": "Tops"
}
},
"attributes": [
{
"id": 1151,
"name": "Size",
"values": [
{
"id": 3871,
"value": "S"
},
{
"id": 3874,
"value": "M"
},
{
"id": 3875,
"value": "L"
}
]
},
{
"id": 1152,
"name": "Gender",
"values": [
{
"id": 3872,
"value": "Male"
},
{
"id": 3873,
"value": "Female"
}
]
}
],
"sku": [
{
"id": 1153,
"stock": 30,
"attributeIds": [
3871,
3872
],
"attributes": [
{
"key": "Size",
"value": "S"
},
{
"key": "Gender",
"value": "Female"
}
]
}
]
}