Pymongo提取文档,修改文档并用replaceone方法更新

2024-09-27 20:20:29 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python更新现有文档而不使用updatemongodb查询

aa = mongodb.findByCollection(resultCollectionName, {})
totalCount = (aa.count())
count = 0
for document in aa:
    values = document["RESULTPERMONTH"].values()
    document["RESULT_SUM"] = sum(values)
    document["RESULT_AVG"] = roundTwoDigits(document["RESULT_SUM"] / len(document["RESULTPERMONTH"])) if len(document["RESULTPERMONTH"]) != 0 else -1000
    document["RESULT_LEN"] = len(document["RESULTPERMONTH"]) 
    document["RESULT_MIN"] = min(values) if len(values) != 0 else -1000
    if "buyInfo" in document:
        del document["buyInfo"]
    if "sellInfo" in document:
        del document["sellInfo"]
    try:
        mongodb.database.get_collection(resultCollectionName).save(document)
    except Exception as ex:
        print(ex)
    count = count +1
    if count % 10000 == 0:
        print(count / totalCount)

这是我以前要更新的代码。由于save方法已被弃用,我想替换它。但我不想使用mongodb查询,而是希望使用python处理

在这种情况下如何使用replaceone


Tags: inlenifmongodbcountresultdocumentelse

热门问题