Model Tree Structures in MongoDB using object references

345 views
Skip to first unread message

Tim McClure

unread,
Mar 20, 2017, 9:30:15 AM3/20/17
to mongodb-user
https://docs.mongodb.com/manual/tutorial/model-tree-structures/

The link above describes how to  model tree structures in MongoDB when the child document is contained in the parent document.  I want to model tree structures in Mongo where the child document has a life cycle different from the parent and can exist without a parent.  Will the parent and children constructs work in Mongo DB wher the children are objects reference to documents in another collection?  Does anyone have any information on modeling tree structures in this fashion?

Thanks,
Tim McClure

Wan Bachtiar

unread,
Mar 30, 2017, 3:16:28 AM3/30/17
to mongodb-user

Will the parent and children constructs work in Mongo DB where the children are objects reference to documents in another collection?

Hi Tim,

The suitability of document model relies heavily on your use case, querying pattern and data workload.

For example, if one of your frequently used queries need to find the parent first, then query for a specific children; storing the children in a separate collection will be more costly for the application compared to embedding the children as sub-documents. However, if your application queries the children information often, but rarely querying the parent (i.e. logical categories ) then it will work for that use case.

MongoDB has a flexible schema, and you should leverage this for the benefit of the application. The key challenge in data modeling is balancing the needs of the application, the performance characteristics of the database engine, and the data retrieval patterns.

Depending on your use case, you may find $lookup aggregation useful to perform parent/children look-up.

Regards,

Wan

Reply all
Reply to author
Forward
0 new messages