我有一个文档在集合中,我想查找文档并更新列表的元素。
以下是示例数据:
{
{
"_id" : ObjectId("5edd3faaf6c9d938e0bfd966"),
"id" : 1,
"status" : "XXX",
"number" : [
{
"code" : "AAA"
},
{
"code" : "CVB"
},
{
"code" : "AAA"
},
{
"code" : "BBB"
}
]
},
{
"_id" : ObjectId("asseffsfpo2dedefwef"),
"id" : 2,
"status" : "TUY",
"number" : [
{
"code" : "PPP"
},
{
"code" : "SSD"
},
{
"code" : "HDD"
},
{
"code" : "IOO"
}
]
}
}
我打算在["AAA", "BBB"]
中找到number.code
"id":1
和价值的位置,number.code
更改为"DDD"
。我用以下代码做到了:
db.test.update(
{
id: 1,
"number.code": {$in: ["AAA", "BBB"]}
},
{
$set: {"number.$[elem].code": "VVV"}
},
{ "arrayFilters": [{ "elem.code": {$in: ["AAA", "BBB"]} }], "multi": true, "upsert": false
}
)
它在 mongodb shell 中工作,但在 python(带pymongo
(中它不会出现以下错误:
raise TypeError("%s must be True or False" % (option,))
TypeError: upsert must be True or False
请帮助我。我能做什么?
pymongo
只是语法有点不同。 它看起来像这样:
db.test.update_many(
{
"id": 1,
"number.code": {"$in": ["AAA", "BBB"]}
},
{
"$set": {"number.$[elem].code": "VVV"}
},
array_filters=[{"elem.code": {"$in": ["AAA", "BBB"]}}],
upsert=False
)
multi
标志不需要update_many
.- 默认情况下
upsert
False
,因此也是多余的。
你可以在这里找到pymongo的文档。