[英]update commands works via mongo shell but not via pymongo
我正在嘗試使用pymongo
更新mongo文檔中的數組,但是它不起作用,但是將相同的查詢復制到robomongo確實可行。 (它返回{'n': 1, 'nModified': 0, 'ok': 1.0, 'updatedExisting': True}
)
roboMongo:
db.my_collection.updateMany(
{'start_time': 1501700400.0},
{'$pull': {'related': {'$in': [{'KEY': '1', 'TYPE': 'my_type'}]}}},
{upsert:true}
)
pymongo代碼:
query_document = {'start_time': 1501700400.0}
update_command = {'$pull': {'related': {'$in': [{'KEY': '1', 'TYPE': 'my_type'}]}}}
_client[db][collection].update_many(query_document, update_command, True)
文獻:
{
"_id" : ObjectId("598570c4ffd387293e368c8d"),
"related" : [
{
"KEY" : "6",
"TYPE" : "my_type"
},
{
"KEY" : "2",
"TYPE" : "my_type"
},
{
"KEY" : "3",
"TYPE" : "my_type"
},
{
"KEY" : "5",
"TYPE" : "my_type"
},
{
"KEY" : "8",
"TYPE" : "my_type"
}
],
"end_time" : 1501621200.0,
"start_time" : 1501700400.0
}
我在想也許與“和”有關?
有什么建議嗎?
謝謝
{'KEY': '1', 'TYPE': 'my_type'}
應該排序,因此我通過執行以下命令來強制排序:
ordered_relateds = []
for ptr in ptrs_to_remove:
ordered_ptrs.append(collections.OrderedDict(sorted(related.items(), key=lambda t: t[0])))
update_command = {"$pull": {"related": {"$in": ordered_related}}}
這樣,KEY將始終是哈希中的第一個元素,而TYPE將是第二個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.