簡體   English   中英

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM