Reputation: 45
I am new to data modelling and am working on a personal project in which users can upload photos to containers. These containers can be nested (for example: "Japan" container may have a sub container called "Cats" in which you can store pictures of all the cats you saw in Japan).
I am imagining 3 entities: Users, Containers, and Photos structured in the following way:
Users Collection:
{
User_id: userId101,
userName: “Chase”,
Email: “[email protected]”,
Containers: [{name: “Thailand”, parentContainer: null}, {name: “Food”, parentContainer:
“Thailand”],
}
Containers Collection:
{
Id: cont_01,
Name: “Thailand”,
ownedBy: userId101
parentContainer: null,
Photos: [“photoId1”, “photoId2”, etc.]
{
Photos Collection:
{
Id: photo_01,
userRef: userId101
Url: “www.unsplash.com/1279178298”
}
I just want to know if I am missing any major detail that will break this before I spend weeks building this out.
Thank you very much :)
Upvotes: 2
Views: 38
Reputation: 49985
There's really multiple factors that can be taken into consideration here. First of all you don't need to think that every "entity" should have a separate collection (like in SQL) BSON can handle nested arrays like below:
{
Id: cont_01,
Name: "Thailand",
ownedBy: userId101
parentContainer: null,
Photos: [ { id: "photo_01", "www.unsplash.com/1279178298" }]
}
"Joining" ($lookup) the data in MongoDB is an additional overhead so keep it merged unless you have a good reason to divide into multiple collections.
Having the data modeled like above gives you a possibility to use $graphLookup to get a tree of parent-child relationships.
This is what I would start with. As a next step you can consider embedding user data into each container (denormalized) to avoid using $lookups
or having single user
document with an array of containers having embedded photos
- more difficult to maintain (updates are a bit more complicated like here) but better read performance since you don't need lookups.
The drawback of having extremely large documents is also 16MB document size limitation which is a lot but it's good to keep that in mind.
Upvotes: 3