How do I decide whether a post is liked by a user with Mongoose How do I decide whether a post is liked by a user with Mongoose mongoose mongoose

How do I decide whether a post is liked by a user with Mongoose


The limit of the document in MongoDB is 16mb, which is pretty huge. Unless you're trying to create the next Facebook, putting the IDs in the array won't be an issue. I have production workflows with 10k+ IDs in a single document array, have no issues. The question should not be about the document size, you have to choose where to put it based on the queries you'll need.

I would create a schema for the Post and put the Liked ids there, such as:

{    title: { type: String },    text: { type: String },    likes: [{ type: ObjectId, ref: 'users' }]}

Posts will get old, people will stop liking it, so the average size of the array if put in Post instead of the User collection should be smaller. Also, putting the ref attribute gives you the ability to use Mongoose Populate API, so you can query for posts such as:

Posts.find().populate('likes');

This will get you the full user information in the array. To search for posts that a single user has likes, is pretty simple too:

Posts.find({likes: userId}).populate('likes');

Hope it helps you.