| Steps To Reproduce: |
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" }
|
|