Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to make a function so that users can like posts (either a post is liked by the user or it is not liked by the user, i.e. no dislike or voting).

My posts are objects in a MongoDB collection with schema

{
  title: String,
  text: String
}

while my users have schema

{
  username: String,
  password: String
}

I could, for instance, create an array at posts with the ids of the users that have liked the specific post. It would look something like

{
  title: String,
  text: String,
  likedByUsers: [ObjectID]
}

or I could create an array at users with the ids of the posts that the user has liked, which would be something like

{
  username: String,
  password: String,
  postsLiked: [ObjectID]
}

However, I expect that the users will like thousands of posts, so I might face a restriction in the size of the objects in MongoDB.

So I am thinking that I should instead create a new collection with likes

{
  userId: ObjectID,
  postId: ObjectID
}

However, I am thinking how I should retrieve it in my app.

Should I store an array of the IDs of all the posts that the user (who is logged in) has liked, and when I am printing each post, I will look if the post is among the posts that the user has liked, and then show an "already liked" button?

Alternatively, I could send the user id when I am requesting to see all the posts, and then for each post add a field saying "isLiked", representing whether an object in the Liked collection could be find, matching both the post and the user id?

like image 329
Jamgreen Avatar asked Jul 11 '17 17:07

Jamgreen


People also ask

What is the __ V field in Mongoose?

In Mongoose the “_v” field is the versionKey is a property set on each document when first created by Mongoose. This is a document inserted through the mongo shell in a collection and this key-value contains the internal revision of the document.24-Jun-2021.

What is the difference between id and _ID in Mongoose?

So, basically, the id getter returns a string representation of the document's _id (which is added to all MongoDB documents by default and have a default type of ObjectId ). Regarding what's better for referencing, that depends entirely on the context (i.e., do you want an ObjectId or a string ).

Is Mongoose still popular?

Mongoose is a Node library for interfacing with MongoDB databases. If you're familiar with the Java landscape, think Hibernate (lib) for MySQL (database engine). You could use MongoDB via a different library, but Mongoose is very popular and well supported.

What does model find () return in Mongoose?

mongoose .find() method returns object with unwanted properties. 12. Model.findOne not returning docs but returning a wrapper object.


1 Answers

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.

like image 182
Luís Brito Avatar answered Oct 13 '22 00:10

Luís Brito