Can I reference a document the id of another document and does it impact performance?

I have a list of documents and each one creates a new, copied document (with some altered data). The new document should refer to the previous document. Should I add a second field (preDoc) with an ObjectID? Because ObjectIDs are working like a primary key, would it be enough to only save the $oid string? Which one performs better or is suited for MongoDB? Or are there even other possibilites to achieve my case?

Curious to know more and thanks in advance!

Hi @borison ,

Interesting question. In that scenario I would not use a “link” list kind of implementation where each document points to the previous but I would tag the related document with one “parent” identifier.

For example :

_id : "doc1",
 text : "...", 
 parent : "group1"
_id : "doc2",
 text : "...", 
 parent : "group1"

Than to get all related documents in the group:

db.collection.find({parent : "group1"}).sort({_id : 1});

Using the following index it can work efficiently : { parent : 1, _id : 1}

Let me know if that is what you were looking for…


I will try it out!

Thank you!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.