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

find_and_modify silently fails to handle sort argument

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4
    • Affects Version/s: 2.3
    • Component/s: None
    • None
    • Environment:
      Mongo 2.2.

      Insert 3 documents:

      coll.insert({'a': 1, 'b': 3, 'c': True})
      coll.insert({'a': 2, 'b': 2, 'c': True})
      coll.insert({'a': 3, 'b': 1, 'c': True})
      

      Find works as expected:

      list(coll.find({'c': True},
                       sort=[('a', pymongo.DESCENDING)]))
      list(coll.find({'c': True},
                       sort=[('a', pymongo.ASCENDING)]))
      

      Find and modify silently mishandles the sort argument:

      coll.find_and_modify(query={'c': True},
                                      update={'$set': {'c': False}},
                                      sort=[('a', pymongo.DESCENDING)])
      Out[529]: {u'_id': ObjectId('506dd39913f8fb2637edf209'), u'a': 1, u'b': 3, u'c': True}
      

      Instead the user must do:

      coll.find_and_modify(query={'c': True},
                                      update={'$set': {'c': False}},
                                      sort={'a': pymongo.DESCENDING})
      

      However this is bad because:
      a) dicts aren't ordered in python (and ordered dict isn't available in older python versions)
      b) the API is inconsistent w.r.t. the find() call:
      sort (optional): a list of (key, direction) pairs specifying the sort order for this query. See sort() for details.
      c) The error occurs silently.

      The sort argument should be sanity checked, and should be consistent.

      The alternative is silent bugs in code (for example a library mongoqueue has a bug because of this inadvertent behaviour):
      http://pypi.python.org/pypi/mongoqueue

      See also confusion on the groups:
      https://groups.google.com/forum/?fromgroups=#!searchin/mongodb-user/pymongo$20find_and_modify$20sort/mongodb-user/rgg9_Sz7_a0/NGM6PYEl1fwJ

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            jblackburn James Blackburn
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: