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

findAndModify() and getLastError() Problem

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.6.5
    • Component/s: None
    • None
    • Environment:
      Mac OS X 10.6.6; MongoDB 1.6.5
    • Fully Compatible
    • ALL

      When using the "findAndModify" command, "getLastError" only returns the result of the last of the two operations. By default, this means it returns the result of the "update" operation (which is normally desirable), but when one uses the "new" option, getLastError returns the result of the "find" operation, so any error in the update clause is inaccessible.

      The following sequence of commands demonstrates the problem:
      db.test.insert(

      {name: 'bob'}

      )
      db.test.findAndModify({query:

      {name: 'bob'}

      , update: {$set:

      {age: 30}

      , $inc: {age: 3}}, new: true})
      db.getLastError()

      Ideally, what you should receive when you run the last line, db.getLastError(), you should get an error "Field name duplication not allowed with modifiers". Instead, you get null, as the find operation (the 2nd of the two internal operations), succeeded. If you had the "new" option set to false, and you had a similar error in the find/query portion of the command, you'd get the same problem.

            Assignee:
            Unassigned Unassigned
            Reporter:
            wardrop Tom Wardrop
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: