Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-585

update() with manipulate=True inserts _id in update document

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • None
    • 2.7
    • None
    • OS X 10.9, MongDB 2.2
    • Major Change

    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)

      1. delete '_id' here
        if '_id' in document:
        del document['_id']

      Attachments

        Activity

          People

            jesse@mongodb.com A. Jesse Jiryu Davis
            Lime Lime YH.Shi [X]
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: