最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I decide whether a post is liked by a user with Mongoose - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jul 11, 2017 at 22:15 styopdev 2,6644 gold badges37 silver badges55 bronze badges asked Jul 11, 2017 at 17:54 JamgreenJamgreen 11.1k32 gold badges122 silver badges231 bronze badges 1
  • If you actually read "How to Model a “likes” voting system with MongoDB" then you will find in the content that for the "current user" and a UI perspective, you only need to know if "that user" liked the post or not. Which is basically an $elemMatch projection on the array. Post.findById(id,{ "likedByUsers": "$elemMatch": { "$eq": userId } }) since all other users would be irrelevant from that users perspective. It is also redundant to also include the posts on the "User" itself. – Neil Lunn Commented Jul 12, 2017 at 2:44
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论