使用 MongoDB $pull 删除数组中的文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15121758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:05:14  来源:igfitidea点击:

Using MongoDB $pull to delete documents within an Array

mongodbpull

提问by VaidAbhishek

I have a collection in MongoDB, which is like following:

我在 MongoDB 中有一个集合,如下所示:

{
    "_id" : "5327010328645530500",
    "members" : [
        {
            "participationCoeff" : 1,
            "tweetID" : "5327010328645530500"
        },
        {
            "participationCoeff" : 1,
            "tweetID" : "2820402625046999289"
        },
        {
            "participationCoeff" : 0.6666666666666666,
            "tweetID" : "6122060484520699114"
        },
        {
            "participationCoeff" : 1,
            "tweetID" : "4656669980325872747"
        }
    ]
}
{
    "_id" : "2646953848367646922",
    "members" : [
        {
            "participationCoeff" : 1,
            "tweetID" : "2646953848367646922"
        },
        {
            "participationCoeff" : 0.75,
            "tweetID" : "7750833069621794130"
        },
        {
            "participationCoeff" : 0.5,
            "tweetID" : "6271782334664128453"
        }
    ]
}

Basically, collection have clusters, where a cluster has an _idfield and a membersfield. Members field is an array of documents, having following format.

基本上,集合有集群,其中集群有一个_id字段和一个members字段。成员字段是一个文档数组,具有以下格式。

{
            "participationCoeff" : 1,
            "tweetID" : "5327010328645530500"
        }

Now, from time to time, I have to delete these sub-documents in members attribute of cluster document, by matching tweetID.

现在,我有时必须通过匹配 tweetID 来删除集群文档的 members 属性中的这些子文档。

However, I'm not able to find a query to achieve this effect. Basically, a tweetID will participate in many clusters, and hence will appear in multiple sub-documents of 'members' attribute of various clusters. I want to supply a bulk $pull operation, where I can remove all the sub-documents in all the clusters (i.e. their members attribute) which match on a particular tweetID.

但是,我无法找到实现此效果的查询。基本上,一个tweetID会参与很多集群,因此会出现在各个集群的'members'属性的多个子文档中。我想提供一个批量 $pull 操作,在那里我可以删除所有集群中的所有子文档(即它们的成员属性),这些子文档与特定的 tweetID 匹配。

Some help and intuition will be really helpful.

一些帮助和直觉将非常有帮助。

回答by JohnnyHK

That's exactly what the $pulloperator does, so in the shell you could use an updatelike:

这正是$pull操作符所做的,所以在 shell 中你可以使用update类似的:

db.clusters.update({}, 
    {$pull: {members: {tweetID: '5327010328645530500'}}}, 
    {multi: true})

Set the multioption so that every document is updated, not just the first one.

设置multi选项以便更新每个文档,而不仅仅是第一个。

回答by Amit Kumar

This will delete multiple tweetsin a single query just pass an array to $in:-

这将multiple tweets在单个查询中删除,只需将数组传递给$in:-

db.clusters.update({}, 
{$pull: {members: {$in: [ {tweetID: '5327010328645530500'},{"tweetID" : "2820402625046999289"} ] } } }, 
{multi: true});

The above method doesnt work in mongoose so for mongoose do:-

上述方法在猫鼬中不起作用,因此对于猫鼬来说:-

 db.clusters.update({}, 
{$pull: {members:[ {tweetID: '5327010328645530500'},{"tweetID" : "2820402625046999289"} ]  } });