繁体   English   中英

更新 mongoDB 对象中数组的选定元素

[英]Update selected elements of array in mongoDB object

我有这样的文件,

{
    "_id": {
        "$oid": "5f33aca82b2fcf5324290ae1"
    },
    "active": true,
    "addresses": [
        {
            "country": "IN",
            "formatted": "xyz",
            "locality": "San Francisco",
            "postalCode": "7656",
            "primary": true,
            "region": "CA",
            "streetAddress": "abc",
            "type": "work"
        },
        {
            "country": "US",
            "formatted": "xyz",
            "locality": "fdfdf",
            "postalCode": "91608",
            "primary": true,
            "region": "CA",
            "streetAddress": "def",
            "type": "other"
        }
    ]
}

地址属性是多值的,我想更新“type”==“work”的所有地址条目的“streetAddress”。

但是当我在下面尝试这个查询时,所有条目的“streetAddress”都得到更新。

mongo.db.test.update_one({'_id': ObjectId('5f33aca82b2fcf5324290ae1'), 'addresses.type':'work'}, {"$set":{'addresses.$[].streetAddress': "mno"}},upsert=True)

结果是,

{
    "_id": {
        "$oid": "5f33aca82b2fcf5324290ae1"
    },
    "active": true,
    "addresses": [
        {
            "country": "IN",
            "formatted": "xyz",
            "locality": "San Francisco",
            "postalCode": "7656",
            "primary": true,
            "region": "CA",
            "streetAddress": "mno",
            "type": "work"
        },
        {
            "country": "US",
            "formatted": "xyz",
            "locality": "fdfdf",
            "postalCode": "91608",
            "primary": true,
            "region": "CA",
            "streetAddress": "mno",
            "type": "other"
        }
    ]
}

正如您所看到的,这两个条目都在被修改。

我正在使用flask-pymongo 库。

$[]将更新数组的元素

但是$[<identifier>]只会更新匹配过滤器的元素

你可以在这里阅读更多

我认为这样的事情应该有效:

mongo.db.test.update_one({'_id': ObjectId('5f33aca82b2fcf5324290ae1')}, 
{"$set":{'addresses.$[addr].streetAddress': "mno"}},
{arrayFilters: [ { "elem.type": 'work' } ]},
upsert=True)
Actual working code on widows 10 Mongo client
//data prep before:
db.test12.insert({
    "active": true,
    "addresses": [
        {
            "country": "IN",
            "formatted": "xyz",
            "locality": "San Francisco",
            "postalCode": "7656",
            "primary": true,
            "region": "CA",
            "streetAddress": "mno",
            "type": "work"
        },
        {
            "country": "US",
            "formatted": "xyz",
            "locality": "fdfdf",
            "postalCode": "91608",
            "primary": true,
            "region": "CA",
            "streetAddress": "mno",
            "type": "other"
        }
    ]
});

--
//data changes post update, you need to use $ to access array elements in the address array
> db.test12.updateOne({"addresses.type":"work"},{$set:{"addresses.$.streetAddress":"St Johns road"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test12.find().pretty();
{
        "_id" : ObjectId("5f492467e551d4c998f3c9ca"),
        "active" : true,
        "addresses" : [
                {
                        "country" : "IN",
                        "formatted" : "xyz",
                        "locality" : "San Francisco",
                        "postalCode" : "7656",
                        "primary" : true,
                        "region" : "CA",
                        "streetAddress" : "St Johns road",
                        "type" : "work"
                },
                {
                        "country" : "US",
                        "formatted" : "xyz",
                        "locality" : "fdfdf",
                        "postalCode" : "91608",
                        "primary" : true,
                        "region" : "CA",
                        "streetAddress" : "mno",
                        "type" : "other"
                }
        ]
}
>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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