{"action":"created","issue":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2660","labels_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2660/labels{/name}","comments_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2660/comments","events_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2660/events","html_url":"https://github.com/LearnBoost/mongoose/issues/2660","id":57006081,"number":2660,"title":"$and modifier failed to work","user":{"login":"leopoldfreeman","id":8280387,"avatar_url":"https://avatars.githubusercontent.com/u/8280387?v=3","gravatar_id":"","url":"https://api.github.com/users/leopoldfreeman","html_url":"https://github.com/leopoldfreeman","followers_url":"https://api.github.com/users/leopoldfreeman/followers","following_url":"https://api.github.com/users/leopoldfreeman/following{/other_user}","gists_url":"https://api.github.com/users/leopoldfreeman/gists{/gist_id}","starred_url":"https://api.github.com/users/leopoldfreeman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leopoldfreeman/subscriptions","organizations_url":"https://api.github.com/users/leopoldfreeman/orgs","repos_url":"https://api.github.com/users/leopoldfreeman/repos","events_url":"https://api.github.com/users/leopoldfreeman/events{/privacy}","received_events_url":"https://api.github.com/users/leopoldfreeman/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/LearnBoost/mongoose/labels/help","name":"help","color":"FFFFFF"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-02-09T09:23:42Z","updated_at":"2015-02-26T04:48:31Z","closed_at":"2015-02-25T19:01:57Z","body":"I want to update one element in the array with multi conditions. I found the '$and' modifer failed to worked properly.\r\n\r\nmongoose v3.8.23\r\nmongodb v2.6.7\r\n\r\nHere is my schema:\r\n```javascript\r\nvar mailInfoSchema = new Schema({\r\n    bodyId: String,\r\n    read: Boolean\r\n});\r\n\r\nvar mailSchema = new Schema({\r\n    userId: String,\r\n    mails: [mailInfoSchema]\r\n});\r\n\r\nvar MailModel = db.model('Mail', mailSchema);\r\n\r\nMail.readMail = function (uid, mailId, cb) {\r\n    MailModel.findOneAndUpdate(\r\n        {\"$and\": [{userId: uid}, {'mails._id': mailId}, {'mails.read': false}]}, // '$and' doesn't work. But works in mongodb shell.\r\n        {\"$set\": {mails.$.read: true}},\r\n        {upsert: false, select: '_id'}, \r\n        cb\r\n    );\r\n};\r\n```\r\n\r\nHere is test data:\r\n```javascript\r\ndb.mail.findOne();\r\n{\r\n\t\"_id\" : ObjectId(\"54d8781ebd4f5e6080eb86eb\"),\r\n\t\"userId\" : ObjectId(\"54d1c7ac78d4693c0ba2652c\"),\r\n\t\"mails\" : [\r\n\t\t{\r\n\t\t\t\"_id\" : ObjectId(\"54d8781ef78fd0887e54c712\"),\r\n\t\t\t\"read\" : false,\r\n                        \"bodyId\": 1\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"_id\" : ObjectId(\"54d8781f997aaf927e9b72be\"),\r\n\t\t\t\"read\" : false,\r\n\t\t\t\"bodyId\" : \"1\"\r\n\t\t}\r\n\t],\r\n\t\"__v\" : 0\r\n}\r\n```\r\n\r\nCall the function at the first time:\r\n```javascript\r\nMail.readMail('54d1c7ac78d4693c0ba2652c', '54d8781ef78fd0887e54c712', cb);\r\n\r\n> db.mail.findOne();\r\n{\r\n\t\"_id\" : ObjectId(\"54d8781ebd4f5e6080eb86eb\"),\r\n\t\"userId\" : ObjectId(\"54d1c7ac78d4693c0ba2652c\"),\r\n\t\"mails\" : [\r\n\t\t{\r\n\t\t\t\"_id\" : ObjectId(\"54d8781ef78fd0887e54c712\"),\r\n\t\t\t\"read\" : true, // Correct\r\n\t\t\t\"bodyId\" : \"1\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"_id\" : ObjectId(\"54d8781f997aaf927e9b72be\"),\r\n\t\t\t\"read\" : false,\r\n\t\t\t\"bodyId\" : \"1\"\r\n\t\t}\r\n\t],\r\n\t\"__v\" : 0\r\n}\r\n```\r\n\r\nFirst time, works fine. Let's call again with the **same parameter**, it should not update any thing, but:\r\n```javascript\r\nMail.readMail('54d1c7ac78d4693c0ba2652c', '54d8781ef78fd0887e54c712', cb);\r\n\r\n> db.mail.findOne();\r\n{\r\n\t\"_id\" : ObjectId(\"54d8781ebd4f5e6080eb86eb\"),\r\n\t\"userId\" : ObjectId(\"54d1c7ac78d4693c0ba2652c\"),\r\n\t\"mails\" : [\r\n\t\t{\r\n\t\t\t\"_id\" : ObjectId(\"54d8781ef78fd0887e54c712\"),\r\n\t\t\t\"read\" : true,\r\n\t\t\t\"bodyId\" : \"1\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"_id\" : ObjectId(\"54d8781f997aaf927e9b72be\"),\r\n\t\t\t\"read\" : true,  // It updated this sub doc!!!\r\n\t\t\t\"bodyId\" : \"1\"\r\n\t\t}\r\n\t],\r\n\t\"__v\" : 0\r\n}\r\n```\r\nLook at the second sub doc, the function updates the wrong doc.\r\n"},"comment":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/comments/76121966","html_url":"https://github.com/LearnBoost/mongoose/issues/2660#issuecomment-76121966","issue_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2660","id":76121966,"user":{"login":"leopoldfreeman","id":8280387,"avatar_url":"https://avatars.githubusercontent.com/u/8280387?v=3","gravatar_id":"","url":"https://api.github.com/users/leopoldfreeman","html_url":"https://github.com/leopoldfreeman","followers_url":"https://api.github.com/users/leopoldfreeman/followers","following_url":"https://api.github.com/users/leopoldfreeman/following{/other_user}","gists_url":"https://api.github.com/users/leopoldfreeman/gists{/gist_id}","starred_url":"https://api.github.com/users/leopoldfreeman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leopoldfreeman/subscriptions","organizations_url":"https://api.github.com/users/leopoldfreeman/orgs","repos_url":"https://api.github.com/users/leopoldfreeman/repos","events_url":"https://api.github.com/users/leopoldfreeman/events{/privacy}","received_events_url":"https://api.github.com/users/leopoldfreeman/received_events","type":"User","site_admin":false},"created_at":"2015-02-26T04:48:31Z","updated_at":"2015-02-26T04:48:31Z","body":"Thanks for your explaination. By the way, it's a simple mail system for our game. Not a real Email system."}}