{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3014","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3014/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3014/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3014/events","html_url":"https://github.com/Automattic/mongoose/issues/3014","id":80849010,"number":3014,"title":"Multiple update queries in one roundtrip","user":{"login":"shaharlinial","id":5569460,"avatar_url":"https://avatars.githubusercontent.com/u/5569460?v=3","gravatar_id":"","url":"https://api.github.com/users/shaharlinial","html_url":"https://github.com/shaharlinial","followers_url":"https://api.github.com/users/shaharlinial/followers","following_url":"https://api.github.com/users/shaharlinial/following{/other_user}","gists_url":"https://api.github.com/users/shaharlinial/gists{/gist_id}","starred_url":"https://api.github.com/users/shaharlinial/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shaharlinial/subscriptions","organizations_url":"https://api.github.com/users/shaharlinial/orgs","repos_url":"https://api.github.com/users/shaharlinial/repos","events_url":"https://api.github.com/users/shaharlinial/events{/privacy}","received_events_url":"https://api.github.com/users/shaharlinial/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Automattic/mongoose/labels/help","name":"help","color":"FFFFFF"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-05-26T08:05:51Z","updated_at":"2015-05-26T16:12:55Z","closed_at":"2015-05-26T16:00:37Z","body":"Apologies if this has been addressed,\r\n\r\nSo the problem as follows:\r\nI had an issue with my schema which led me to ask a question on stackoverflow:\r\n(Below there is a short version of the question)[Question on StackOverflow](http://stackoverflow.com/questions/30442638/conditional-update-mongodb-set-and-addtoset)\r\n\r\nThis is the schema:\r\n\r\n    uid:{type:ObjectId},\r\n    username: {type: String, unique: true, required: true},\r\n    devices: [\r\n        {\r\n            ip:{type:String},\r\n            meta:{type:String},\r\n            type: {type: String, default: 'none', enum: ['android', 'ios', 'web', 'none']},\r\n            cnuid: {type: String},\r\n            uuid:{type:String, unique:true}\r\n        }\r\n    ]\r\n\r\n\r\nThese are the rules:\r\n**UUID** is a unique and one time created per device.\r\n\r\n**CNUID** is a unique number also, but consider it as a token, which means it updates from time to time.\r\n\r\n*If username does not exist, create a username and attach its device to this array,*\r\n*If it exists try to attach this device to the array, as long as there isn't a uuid like this already,*\r\n*If there is a uuid like this overwrite the cnuid with the cnuid provided.*\r\n\r\nApparently the only solution for this according to the answer, and to the fact that you can't use $set and $addToSet on the same attribute([Feature request on Mongodb Jira](https://jira.mongodb.org/browse/SERVER-6566)), is that you must run 3 Idempotence queries.\r\n\r\nwhich are explained here [StackOverflow Answer for the issue above](http://stackoverflow.com/questions/30292159/update-a-mongodb-subdocument-when-the-parent-document-might-not-exist)\r\n\r\nWhat's left for me is to perform those 3 queries in one update command by db.\r\nI'm kinda trying to avoid using the native driver.\r\n\r\nThis is the straight db.update command : \r\nExplained here [MongoDB Update](http://docs.mongodb.org/manual/reference/command/update/#dbcmd.update)\r\nand looks like so:\r\n\r\n    {\r\n        update: <collection>,\r\n        updates:\r\n        [\r\n            { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },\r\n            { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },\r\n            { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },\r\n            ...\r\n        ],\r\n        ordered: <boolean>,\r\n        writeConcern: { <write concern> }\r\n    }\r\n\r\n\r\nIs this already something you are familiar with?"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/105581878","html_url":"https://github.com/Automattic/mongoose/issues/3014#issuecomment-105581878","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3014","id":105581878,"user":{"login":"shaharlinial","id":5569460,"avatar_url":"https://avatars.githubusercontent.com/u/5569460?v=3","gravatar_id":"","url":"https://api.github.com/users/shaharlinial","html_url":"https://github.com/shaharlinial","followers_url":"https://api.github.com/users/shaharlinial/followers","following_url":"https://api.github.com/users/shaharlinial/following{/other_user}","gists_url":"https://api.github.com/users/shaharlinial/gists{/gist_id}","starred_url":"https://api.github.com/users/shaharlinial/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shaharlinial/subscriptions","organizations_url":"https://api.github.com/users/shaharlinial/orgs","repos_url":"https://api.github.com/users/shaharlinial/repos","events_url":"https://api.github.com/users/shaharlinial/events{/privacy}","received_events_url":"https://api.github.com/users/shaharlinial/received_events","type":"User","site_admin":false},"created_at":"2015-05-26T16:12:55Z","updated_at":"2015-05-26T16:12:55Z","body":"This can't be really done with `updateMany();` update many supports in one query to fetch more than one document.\r\n\r\nIn my case I have to use multiple queries which are different from one another,"}}