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

findAndModify fail to addToSet a field, while querying by the same field.

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Duplicate
    • 2.6.2
    • None
    • None
    • None
    • ALL
    • Hide

      1. create a new empty collection "users"
      2. Execute the following query, should fail

      > db.users.findAndModify({query: {emails:"test@gmail.com"}, update:{"$addToSet": {"emails": "test@gmail.com"}, "$set": {"name": "Tamer"}}, upsert: true})
      2014-09-12T22:25:15.414+0300 findAndModifyFailed failed: {
      	"value" : null,
      	"errmsg" : "exception: Cannot apply $addToSet to a non-array field. Field named 'emails' has a non-array type String in the document INVALID-MUTABLE-ELEMENT",
      	"code" : 16836,
      	"ok" : 0
      } at src/mongo/shell/collection.js:614

      3. Execute this query, should pass

      > db.users.findAndModify({query: {emails: {$elemMatch: { $regex: 'test@gmail.com', $options: 'i' }}}, update:{"$addToSet": {"emails": "test@gmail.com"}, "$set": {"name": "Tamer"}}, upsert: true})
      null
      > db.users.find()
      { "_id" : ObjectId("541349cd912259e2bc9c311b"), "emails" : [ "test@gmail.com" ], "name" : "Tamer" }

      Show
      1. create a new empty collection "users" 2. Execute the following query, should fail > db.users.findAndModify({query: {emails:"test@gmail.com"}, update:{"$addToSet": {"emails": "test@gmail.com"}, "$set": {"name": "Tamer"}}, upsert: true}) 2014-09-12T22:25:15.414+0300 findAndModifyFailed failed: { "value" : null, "errmsg" : "exception: Cannot apply $addToSet to a non-array field. Field named 'emails' has a non-array type String in the document INVALID-MUTABLE-ELEMENT", "code" : 16836, "ok" : 0 } at src/mongo/shell/collection.js:614 3. Execute this query, should pass > db.users.findAndModify({query: {emails: {$elemMatch: { $regex: 'test@gmail.com', $options: 'i' }}}, update:{"$addToSet": {"emails": "test@gmail.com"}, "$set": {"name": "Tamer"}}, upsert: true}) null > db.users.find() { "_id" : ObjectId("541349cd912259e2bc9c311b"), "emails" : [ "test@gmail.com" ], "name" : "Tamer" }

    Description

      Read Steps To Reproduce first.
      I think findAndModify with upsert flag in case the object is not found, uses the query as the schema of the object. Query like:

      {emails:"test@gmail.com"}

      makes Mongodb thinks emails field contain string (although the collection is empty). While query like {$elemMatch: { $regex: 'test@gmail.com', $options: 'i' }} works just fine.

      I tested only on v2.6.2

      Note: Replacing $addToSet with $push, results in the same issues.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              tabdulradi Tamer Mohammed AbdulRadi
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: