Link two document with native driver

Link two document with native driver

Problem

I have two different collections in my database, and at this point I need to link a document from first collection to a document form second collection with native NodeJs driver.

How can I do that? Should I hold the _id from first collection in second collection?

Problem courtesy of: Afshin Mehrabani

Solution

Yes, I would suggest storing the _id of the related document, manually, as you described. I’ve done that a lot, and it works well.

A more complex (and seemingly unnecessary in the case you describe), would be to use a DBRef.

Solution courtesy of: WiredPrairie

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *