如何删除mongodb字段中的特定数据
我有这个帖子集合:
{
id: 'post_id',
content: 'text here',
postedBy: 'user_id',
likes: [
{
0: 'user_id
},
{
1: 'another_user_id'
}
]
}
我希望能够删除特定用户在所有帖子中的所有喜欢。我尝试过使用 $unset
运算符,如下所示:
const likes = await Post.updateMany({ $unset: { likes: user_id } });
和这样:
const likes = await Post.updateMany(
{ likes: user_id },
{ $unset: { likes: '' } }
);
但是,这两者的作用都是从包含该用户的“喜欢”的帖子中删除所有“喜欢”。
有人知道我如何只能删除某个特定用户的点赞吗?
I have this posts collection:
{
id: 'post_id',
content: 'text here',
postedBy: 'user_id',
likes: [
{
0: 'user_id
},
{
1: 'another_user_id'
}
]
}
I would like to be able to delete all likes by a specific user across all posts. I have tried using the $unset
operator like this:
const likes = await Post.updateMany({ $unset: { likes: user_id } });
And like this:
const likes = await Post.updateMany(
{ likes: user_id },
{ $unset: { likes: '' } }
);
However what these both do is remove all the likes from posts that contain a like by this user.
Does anybody know how I can only remove the likes that one particular user has made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是使用
$ pull
而不是$ unset
:The answer was to use
$pull
instead of$unset
: