Details
Description
when manipulate is set False, calling collection.update(spec, document, manipulate=False) is ok. But when manipulate is set to True, calling collection.update(spec, document, manipulate=True) raise Exception.
here is the test code.
# -*- coding: utf-8 -*-
|
import pymongo
|
|
class MySONManipulator(pymongo.son_manipulator.SONManipulator):
|
def transform_incoming(self, son, collection):
|
print son
|
return son
|
|
def test_pymongo():
|
client = pymongo.MongoClient()
|
db = client['test']
|
# db.add_son_manipulator(MySONManipulator())
|
collection = db['test']
|
|
collection.remove()
|
collection.insert({'name': 'a'})
|
collection.update({'name': 'a'}, {'word': 'hello world'}, manipulate=True)
|
|
test_pymongo()
|
it raise: pymongo.errors.OperationFailure: cannot change _id of a document old:
{ _id: ObjectId('525977b27379180706451bb4'), name: "a" }new:
{ _id: ObjectId('525977b27379180706451bb5'), word: "hello world" }.
And i found one solution is to delete field '_id' of document in method update().
if manipulate:
document = self.__database._fix_incoming(document, self)
- delete '_id' here
if '_id' in document:
del document['_id']