Reputation: 73
For those of you who worked with Firebase (Firestore), then you can have a collection with documents in which each document has an id, then a collection can hold a sub collection (equivalent of an embedded document as an array of documents as a property).
Then, this sub collection can hold many documents while each has an id.
In Firestore the sub collection is lazy loaded.
It will fetch the documents on that collection, but if there is one or more sub collections, it won't retrieve it unless specifically going to that route. e.g: collection/document/subcollectionname/anotherdocument
So 2 questions:
EDIT: I currently have a firestore implementation which has a subcollection/s practice behind it.
Example: organization => documentId => projects => projectId => activities => :activityType => activityId
Now, the good thing about it is that if I choose to get all organizations, then I will get only the documents of the organization collection and NOT the embedded subcollections (projects, etc..) while in MongoDB, I would get EVERYTHING per document.
How do I achieve the same nested documents with their own nested documents structure with the lazy load effect in MongoDB?
Upvotes: 7
Views: 2967
Reputation: 50830
How many activities can a single project have? If there's no limit then you're better off creating a root level collection for activities. In MongoDB, the maximum BSON document size is 16 MB. That being you may not be able to store all projects and their activities in a single document (organization document).
I would create 3 collections namely - organizations, projects and activities.
I've added those additional organizationID
, projectID
fields even though you have _id
just in case you'd like to have Firestore Document IDs there for easier side-by-side queries.
You don't have to worry about 16 MB document size limit this way and it'll be easier to query both projects and activities as long as you have the correct IDs.
Querying activities of a certain project:
await db.collection("activities").find({projectID: "myProjectID"}).toArray()
Thereafter it's upto you how you want to write queries with projections, aggregation, etc.
Upvotes: 5