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

Document not found when using an ObjectId spec

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6
    • Component/s: None
    • Environment:
      pymongo 2.6

      Quering a document using an instance of ObjectId does not return a result even if the document exists.

      Try this in python console:

      import pymongo
      from bson import ObjectId
      client = pymongo.MongoClient('localhost', 27017)
      db = client.local

      db.test.insert(

      {"_id": "51c9d99649b5d534db8d483a", "var": "foo"}

      )

      try:
      doc = db.test.find_one(

      {'_id': ObjectId('51c9d99649b5d534db8d483a')}

      )
      assert (doc != None)
      except:
      print 'Document not found!'

      try:
      doc = db.test.find_one(

      {'_id': str(ObjectId('51c9d99649b5d534db8d483a'))}

      )
      assert (doc != None)
      except:
      print 'Document not found!'

      Using the string representation of ObjectId returns the spected result.

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            chrramirez Christopher Ramírez
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: