From the course: Data Modeling in MongoDB

Unlock the full course today

Join today to access over 25,200 courses taught by industry experts.

One-to-many relationships with back references

One-to-many relationships with back references - MongoDB Tutorial

From the course: Data Modeling in MongoDB

One-to-many relationships with back references

- [Instructor] Our application as we know it to this point has two major use cases for us to consider. One, all posts written by a given user for example, your homepage. And secondly all post a given user should see for example, your feed which has the most recent posts from all the people you know. In both use cases we have an array of posts that we have to deal with that are attached to a user in some meaningful way. Let's take a look at how we've modeled that so far. The current state of our model takes care of the one to many case by storing an array of post IDs with the user. And this gives us two ways to fetch all the posts for a user. We can use the array of post IDs and run a query that fetes them all. Much like you would do a normal in query in a SQL database. In other words give me a list of all the posts with these IDs. The alternative to that, is to use an aggregation pipeline with a dollar lookup which acts…

Contents