Document referencing within the same collection

Hello everyone. Need an advise from MongoDB gurus.

I have a collection ‘companies’ which is the list of legal entities. Each entity is unique: all of them have their own official legal name, registration number, and other unique identifiers. And yet, some of them can have parent-child relationship between each other from the point of view ownership: one company can be a shareholder in another one and thus considered as parent. Since all the companies are quite unique I don’t think that imbedding child companies as an array of documents into the document of parent company would be a good idea. It’s not kind of “product-review” or “author-publisher” relationship which you can find in a lot of documentations and examples on this topic.

So, could you please share the best practices of handling such kind of relationships in MongoDB?

Thanks in advance,
Max

One simple solution would be an array of the child _id’s or registration number ( or any other unique value) instead of the entire document. Then you could do a query on the values in the array to find the child companies. This shouldn’t have an issue of mutable growing arrays since the growth will be small. But if the array will have a lot of child companies this could be an issue.

This link from the mongodb data model documentation shows a similar example: https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/

Thanks for your reply @tapiocaPENGUIN . The example that you’ve kindly shared is about referencing between 2(!) different collections (book - publisher). Can I do something similar to it within the same(!) collection? I tried to experiment a little bit with storing parent company’s id in child company’s document, but it didn’t work for me (or I might be doing something wrong)

Hello @Maxim_Mitrokhin, welcome to the MongoDB Community forum.

The way to maintain the data in a collection depends upon factors. How do you want to query or access the data? The important queries drive the modeling of the data.

Your companies collection data could be like any one of the following or may be different. There may be one or more owner companies, and one or more children companies (or no owner or children at all). For example:

Example 1:

{ "_id" : 1, "name" : "comp-1" }
{ "_id" : 2, "name" : "comp-2", "ownerCompanies" : [ "comp-1" ] }
{ "_id" : 3, "name" : "comp-3", "ownerCompanies" : [ "comp-1", "comp-2" ] }

Example 2:

{ "_id" : 1, "name" : "comp-1", "childCompanies": [ "comp-2", "comp-3" ]  }
{ "_id" : 2, "name" : "comp-2" }
{ "_id" : 3, "name" : "comp-3" }

See this document on Model Tree Structures. You might be interested with Model Tree Structures with Parent References and Model Tree Structures with Child References.

Referencing documents within the same collection is generally legal, as long as it adheres to copyright and fair use regulations. Proper citation and limited use for educational or transformative purposes are key considerations to avoid infringement issues.