[英]MongoDB update $pull document from multiple arrays
I have a document with the following structure (simplified): 我有一个具有以下结构的文档(简化了):
{
"containers": [
{
"containerId": 1,
"components": ["component1", "component2"]
},
{
"containerId": 2,
"components": ["component3", "component1"]
}]
}
How would one write a query that removes "component1" from BOTH containers? 一个人如何编写查询以从两个容器中删除“ component1”? Is this possible?
这可能吗?
So far I've tried doing {"$pullAll": { "containers.$.component": ["component1"]}}
, a similar query with $pull
, setting multi: true
but I always end up removing the component from the first array only (I'm using .update()
) 到目前为止,我已经尝试过执行
{"$pullAll": { "containers.$.component": ["component1"]}}
,这与$pull
类似,设置了multi: true
但是我总是最终从中删除该组件仅第一个数组(我正在使用.update()
)
EDIT: Raw data ahead! 编辑:原始数据!
{
"_id" : ObjectId("53a056cebe56154c99dc950b"),
"_embedded" : {
"click" : {
"items" : [],
"_links" : {
"self" : {
"href" : "http://localhost/v1/click"
}
}
},
"container" : {
"_links" : {
"self" : {
"href" : "http://localhost/v1/container"
}
},
"items" : [
{
"name" : "Container test",
"uriName" : "Container_test",
"description" : "this is a test container",
"containerId" : "CONTAINER TEST+SITE_TEST",
"component" : [
"ANOTHER COMPONENT+SITE_TEST",
"ANOTHER COMPONENT+SITE_TEST",
"SARASA+SITE_TEST"
],
"_links" : {
"self" : {
"href" : "http://localhost/v1/container/CONTAINER TEST+SITE_TEST"
}
}
},
{
"name" : "sasasa",
"uriName" : "sasasa",
"description" : "container description",
"containerId" : "SASASA+SITE_TEST",
"component" : [
"ANOTHER COMPONENT+SITE_TEST",
"COMPONENT+SITE_TEST",
"FAFAFA+SITE_TEST",
"SARASA+SITE_TEST"
],
"_links" : {
"self" : {
"href" : "/v1/container/SASASA+SITE_TEST"
}
}
}
]
}
},
"name" : "SITE_TEST",
"siteId" : "SITE_TEST",
"url" : "/v1/site"
}
Ok, so what I'm trying to do is remove the component "SARASA+SITE_TEST" from the two containers. 好的,所以我想做的是从两个容器中删除组件“ SARASA + SITE_TEST”。 I'm using robomongo to test the queries.
我正在使用robomongo来测试查询。 I've tried
db.site.update({"_embedded.container.items.component": "SARASA+SITE_TEST"},{"$pullAll": { "_embedded.container.items.component": ["SARASA+SITE_TEST"]}}, {multi: true})
and it didn't work, previously I've tried db.site.update({"_embedded.container.items.component": "SARASA+SITE_TEST"},{"$pull": { "_embedded.container.items.$.component": "SARASA+SITE_TEST"}}, {"multi": true})
and it didn't work either. 我已经尝试
db.site.update({"_embedded.container.items.component": "SARASA+SITE_TEST"},{"$pullAll": { "_embedded.container.items.component": ["SARASA+SITE_TEST"]}}, {multi: true})
,它没有用,以前我尝试过db.site.update({"_embedded.container.items.component": "SARASA+SITE_TEST"},{"$pull": { "_embedded.container.items.$.component": "SARASA+SITE_TEST"}}, {"multi": true})
,但也没有用。 I assume robomongo exposes the mongo driver directly, I didn't try to run this from the command line. 我假设robomongo直接公开mongo驱动程序,我没有尝试从命令行运行它。
(the document is a "site", that's why my queries start with db.site) (文档是一个“站点”,这就是为什么我的查询以db.site开头的原因)
I had a similar problem and I tried $pullAll and it worked. 我有一个类似的问题,我尝试了$ pullAll并且它起作用了。
https://docs.mongodb.org/manual/reference/operator/update/pullAll/ https://docs.mongodb.org/manual/reference/operator/update/pullAll/
I tried the simplified version of your data and $pull works: 我尝试了您的数据的简化版本,并且$ pull起作用了:
> db.testcoll.insert({"containers": {"containreId": 1, "components": ["component1", "component2"]}})
> db.testcoll.insert({"containers": {"containreId": 2, "components": ["component3", "component1"]}})
> db.testcoll.find()
{ "_id" : ObjectId("53a8428ca2696f063b5c51eb"), "containers" : { "containreId" : 1, "components" : [ "component1", "component2" ] } }
{ "_id" : ObjectId("53a8429ea2696f063b5c51ec"), "containers" : { "containreId" : 2, "components" : [ "component3", "component1" ] } }
> db.testcoll.update({"containers.components": "component1"}, {$pull: {"containers.components": "component1"}}, {multi: true})
> db.testcoll.find()
{ "_id" : ObjectId("53a8428ca2696f063b5c51eb"), "containers" : { "components" : [ "component2" ], "containreId" : 1 } }
{ "_id" : ObjectId("53a8429ea2696f063b5c51ec"), "containers" : { "components" : [ "component3" ], "containreId" : 2 } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.