简体   繁体   中英

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? 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() )

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. I'm using robomongo to test the queries. 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. I assume robomongo exposes the mongo driver directly, I didn't try to run this from the command line.

(the document is a "site", that's why my queries start with db.site)

I had a similar problem and I tried $pullAll and it worked.

https://docs.mongodb.org/manual/reference/operator/update/pullAll/

I tried the simplified version of your data and $pull works:

> 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 } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM