Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1572

If there are no matches, findandmodify() should return an empty result, not raise an exception.

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.5.6
    • Component/s: None
    • None
    • Environment:
      Ubuntu 10.4, x86-64
    • ALL

      See comments on (closed) PYTHON-152

      findandmodify() raises pymongo.errors.OperationFailure when there are no matches:

      File "build/bdist.linux-x86_64/egg/pymongo/database.py", line 294, in command
      pymongo.errors.OperationFailure: command SON([('findandmodify', 'my_queue'), ('sort',

      {'_id': -1}

      ), ('query', {'started': {'$exists': False}}), ('update', {'$set': {'started': 1280965374}})]) failed: No matching object found

      According to the mongodb documentation for findandmodify:

      "This command can be used to atomically modify a document (at most one) and return it."

      This implies that findandmodify() behaves much like the find() command--it is perfectly acceptable to not have a match at all.
      Such behavior makes perfect sense. I do not believe it should constitute a command failure simply because there are no matches.
      No matches should return an empty result, not raise a command failure exception.

      IMO, command failure, should be when it finds a match but fails to make the modification, or some other actual but unexpected condition occurs.

      This is a very common use case--a consumer pulling things off a queue. When the queue is empty, it shouldn't be generating an exception, just returning empty. It yields much nicer code.

            Assignee:
            mathias@mongodb.com Mathias Stearn
            Reporter:
            nick@leippe.com Nick Leippe
            Votes:
            3 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: