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

Collection.find() fails when the filter is a defaultdict

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.0
    • Affects Version/s: 3.0
    • Component/s: None
    • None
    • Environment:
      mongodb 2.4.9, Ubuntu 14.04, pymongo 3.0rc0

      collection.find() fails when the filter dict is a defaultdict. find() adds a new key of "_id" which causes the filter to never match. This worked with pymongo 2.8.

      Sample session with pymongo 3.0rc0. The filter is a defaultdict(dict) and after the find fails you can see the filter has had '_id': {} added to it.

      >>> from collections import defaultdict
      >>> import pymongo
      >>> pymongo.version
      '3.0rc0'
      >>> db = pymongo.MongoClient().test
      >>> db.test.insert({"foo": "bar"})
      ObjectId('5515994451c3560ed8deb8fc')
      >>> q = {"foo": "bar"}
      >>> next(db.test.find(q), "NOTFOUND")
      {u'_id': ObjectId('5515994451c3560ed8deb8fc'), u'foo': u'bar'}
      >>> d = defaultdict(dict)
      >>> d.update(q)
      >>> next(db.test.find(d), "NOTFOUND")
      'NOTFOUND'
      >>> d
      defaultdict(<type 'dict'>, {'foo': 'bar', '_id': {}})
      

      A similar session with pymongo 2.8 works:

      >>> from collections import defaultdict
      >>> import pymongo
      >>> pymongo.version
      '2.8'
      >>> db = pymongo.MongoClient().test
      >>> db.test.insert({"foo": "bar"})
      ObjectId('55159b3651c3560f70111125')
      >>> q = {"foo": "bar"}
      >>> next(db.test.find(q), "NOTFOUND")
      {u'_id': ObjectId('55159b3651c3560f70111125'), u'foo': u'bar'}
      >>> d = defaultdict(dict)
      >>> d.update(q)
      >>> next(db.test.find(d), "NOTFOUND")
      {u'_id': ObjectId('55159b3651c3560f70111125'), u'foo': u'bar'}
      >>> d
      defaultdict(<type 'dict'>, {'foo': 'bar'})
      

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            rectalogic Andrew Wason
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: