I'm learning about appery's DB which is based on mongodb. I need something like a one-to-many relationship. E.g : Level and Units in a high-rise apartment.
Each level will have many units. Each unit will hv few properties such as Size, Address, Owner name, etc. I want to retrieve a list of all the levels and their units.
I tried to embed the units array into the Level collection as an array.
However, I feel it's not suitable as the array will handle more and more properties in future. Hence, as mongodb is not a relational database, I want to try the pointer.
My idea :
Create 2 collections (Levels and Units).
Create pointer in Units collection to point to Level
Query for all units in Units collection (server code)
Group the units to levels using the pointer. Then, generate JSON array which is grouped by Level. (server code)
Return response for mapping to UI.
Preferred return result :
{
_id: "54332433423a23342",
name: "Level 1",
units: [
{
address: "A-1-1",
size: "1200",
owner: "John",
},
{
address: "A-1-2",
size: "1140",
owner: "Doe",
}
]
}
{
_id: "54332433423a23342",
name: "Level 2",
units: [
{
address: "B-2-1",
size: "1200",
owner: "Alex",
},
{
address: "B-2-2",
size: "1140",
owner: "Chang",
}
]
}Is there any example or can someone provide a sample server code to do step (4)?