'Best way to create a Mongoose model, reference it to a different model, and query it

I have been using Laravel for a while and thus familiar with structured query language, where we just create relationships by assigning foreign keys. Now, I am very interested in NodeJS and mongoose. I have actually worked with them and have built web apps with them. But I have mostly used the embedded method for referencing (if I am allowed to use the word with this method.), but I feel like it is not an optimal way to handle it, especially if the app is a large one.

Thus, I became curious about the referencing method. One thing that I can't wrap my head around yet, is how to reference two other models in one model.

For instance, if I have a User model and a Post model, and I decide to have users react to a post. Instead of have the likes as a field on the Post Schema, I'd like to have the like as its own model, then reference the liked Post, and the User that liked it. So when the post is fetched, I want to query the Like model for the post and the users that liked it.

What would be the best way to design this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source