![](/img/trans.png)
[英]How to update field “type” for all documents in mongodb collection using python mongodb client library (pymongo)
[英]pymongo update all documents in collection for field with it's substring value
{'some': 'abcdefg', 'some2': 'hijklmn'}
期望輸出
{'some': 'abc', 'some2': 'hij'}
換句話說,子串 [:3]
我怎樣才能用 pymongo 做到這一點
您可以將 updatemany 與聚合一起使用: https ://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/
update_pipe = [
{ "$set":
{ "some":
{ "$substr": ['$some', 0,3]}
},
},
{ "$set":
{ "some2":
{ "$substr": ['$some2', 0,3]}
},
}
]
collection.update_many({}, update_pipe)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.