我如何决定一个帖子是否被一个有猫鼬的用户喜欢

时间:2022-03-06 04:43:45

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

我的post是MongoDB集合中的对象,带有模式

{
  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

例如,我可以在贴子上创建一个数组,其中包含喜欢特定贴子的用户的id。看起来是这样的

{
  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

或者我可以用用户喜欢的文章的id为用户创建一个数组,就像这样

{
  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.

但是,我期望用户会喜欢成千上万的文章,因此我可能会面临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?

我是否应该存储用户(已登录)喜欢的所有帖子的id数组,当我打印每个帖子时,我将查看该帖子是否在用户喜欢的帖子中,然后显示一个“已经喜欢”按钮?

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?

或者,我可以在请求查看所有帖子时发送用户id,然后为每个帖子添加一个“islike”字段,表示是否可以找到like集合中的对象,匹配post和user id。

1 个解决方案

#1


2  

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.

MongoDB中的文档限制是16mb,非常大。除非你想创建下一个Facebook,否则把id放到数组中不会有问题。我在一个文档数组中有10k+ id的生产工作流,没有问题。问题不应该是关于文档的大小,您必须根据需要的查询选择放在哪里。

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

我会为文章创建一个模式,并将喜欢的id放在那里,例如:

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

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

文章会变老,人们会不再喜欢它,所以如果将数组的中等大小放在Post而不是User集合,应该会更简单。另外,将ref属性设置为可以使用Mongoose Populate API,这样您就可以查询诸如:

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.

希望它能帮助你。

#1


2  

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.

MongoDB中的文档限制是16mb,非常大。除非你想创建下一个Facebook,否则把id放到数组中不会有问题。我在一个文档数组中有10k+ id的生产工作流,没有问题。问题不应该是关于文档的大小,您必须根据需要的查询选择放在哪里。

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

我会为文章创建一个模式,并将喜欢的id放在那里,例如:

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

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

文章会变老,人们会不再喜欢它,所以如果将数组的中等大小放在Post而不是User集合,应该会更简单。另外,将ref属性设置为可以使用Mongoose Populate API,这样您就可以查询诸如:

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.

希望它能帮助你。