[英]Update selected elements of array in mongoDB object
I have document like this,我有这样的文件,
{
"_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"
}
]
}
The address attribute is multivalued , i want to update "streetAddress" of all address entries where "type"=="work".地址属性是多值的,我想更新“type”==“work”的所有地址条目的“streetAddress”。
But when i try this query below, "streetAddress" of all entries are getting updated.但是当我在下面尝试这个查询时,所有条目的“streetAddress”都得到更新。
mongo.db.test.update_one({'_id': ObjectId('5f33aca82b2fcf5324290ae1'), 'addresses.type':'work'}, {"$set":{'addresses.$[].streetAddress': "mno"}},upsert=True)
the result is,结果是,
{
"_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"
}
]
}
as you can see both entries are getting modified.正如您所看到的,这两个条目都在被修改。
I am using flask-pymongo library.我正在使用flask-pymongo 库。
$[]
will update elements of the arrays $[]
将更新数组的元素
But $[<identifier>]
will update only elements that match the filter但是$[<identifier>]
只会更新匹配过滤器的元素
You can read more here你可以在这里阅读更多
I think something like this should work :我认为这样的事情应该有效:
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.