问题描述
我在MongoDB中有一个集合,如下所示:
I have a collection in MongoDB, which is like following:
{
"_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"
}
]
}
基本上,集合具有群集,其中群集具有_id
字段和members
字段.成员字段是一系列文档,格式如下.
Basically, collection have clusters, where a cluster has an _id
field and a members
field. Members field is an array of documents, having following format.
{
"participationCoeff" : 1,
"tweetID" : "5327010328645530500"
}
现在,我不得不不时地通过匹配tweetID删除群集文档的member属性中的这些子文档.
Now, from time to time, I have to delete these sub-documents in members attribute of cluster document, by matching tweetID.
但是,我无法找到查询来实现此效果.基本上,tweetID将参与许多群集,因此将出现在各个群集的成员"属性的多个子文档中.我想提供一个批量$ pull操作,在这里我可以删除所有集群中所有与特定tweetID相匹配的子文档(即它们的member属性).
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.
一些帮助和直觉将非常有帮助.
Some help and intuition will be really helpful.
推荐答案
这正是$pull
运算符的作用,因此在shell中可以使用update
,如:
That's exactly what the $pull
operator does, so in the shell you could use an update
like:
db.clusters.update({},
{$pull: {members: {tweetID: '5327010328645530500'}}},
{multi: true})
设置multi
选项,以便更新每个文档,而不仅仅是第一个.
Set the multi
option so that every document is updated, not just the first one.
这篇关于使用MongoDB $ pull删除数组中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!