简体   繁体   中英

How to store many-to-many relationships in MongoDB

I think its fair to say I'm pretty new to MongoDB having just installed the database this afternoon.

I'm managing to get to grips with storing and retrieving objects but am struggling to find the best way of storing objects that have a many-to-many relationship.

I've already come accross the DBRef object and have got that working but this seems to only support a lazy loaded approach.

Is there a way to encourage MongoDB to eagerly load DBRefs?
Is there a better/different way to store the many-to-many relationship?

Many thanks

Rob

So first, I think you need to look at this question over here , which talks about many-to-many relationships.

The other thing to understand is the nature of "DBRefs". The MongoDB database does not provide any join functionality .

The DBRef is just a standard that several library / driver implementers agreed upon a couple of years ago. The DBRef is just a JSON object in a specific notation that provides a pointer to some other document in some collection. So the implementation of "lazy vs. eager" loading is completely specific to the driver / wrapper library that you are using.

That stated, the concept of "eager loading" is rather pointless with MongoDB. In SQL you can potentially save on total queries by using some form of eager loading and doing a join "in advance". Again, the DB does not support joins, so "eager loading" takes the same number of queries as "lazy loading".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM