BanksySan
BanksySan

Reputation: 28510

Exception when calling replace_one in Python

I'm trying to replace a document.

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})

However, when I run this I get:

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

What's going here? My method for replace_one looks the same as the one in the pymongo docs.

Upvotes: 0

Views: 1150

Answers (1)

alecxe
alecxe

Reputation: 473873

See this comma between the items, you are passing in a set:

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

But should have passed a dictionary:

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

Same goes for the replacement document - {"foo": 1} instead of {"foo", 1}.

Upvotes: 4

Related Questions