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

Python 更新mongoDB对象中数组的选定元素,python,mongodb,pymongo,Python,Mongodb,Pymongo,我有这样的文件 { "_id": { "$oid": "5f33aca82b2fcf5324290ae1" }, "active": true, "addresses": [ { "country": "IN", "formatted&q

我有这样的文件

{
    "_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"
        }
    ]
}
address属性是多值的,我想更新所有地址条目的“streetAddress”,其中“type”=“work”

但当我尝试下面的查询时,所有条目的“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库。

$[]
将更新数组的元素

但是
$[]
将只更新与筛选器匹配的元素

你可以读更多

我认为这样做应该奏效:

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