在Python中调用replace_one时出现异常

时间:2016-04-02 15:21:56

标签: python pymongo

我正在尝试更换文件。

import pymongo

connection = pymongo.MongoClient("mongodb://localhost:27017")

db = connection.test
collection = db.foo

query = {}

try:
    cursor = collection.find(query)
except Exception as e:
    print "Exception: ", type(e), e

for doc in cursor:
    collection.replace_one({"_id", doc["_id"]}, {"foo", 1})

然而,当我跑步时,我得到:

TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping

这是怎么回事?我replace_one的方法与pymongo docs中的方法相同。

1 个答案:

答案 0 :(得分:4)

在项目之间看到此逗号,您正在传递

{"_id", doc["_id"]}

但是应该通过字典

{"_id": doc["_id"]}

替换文档也是如此 - {"foo": 1}而不是{"foo", 1}