{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/2864","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/2864/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/2864/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/2864/events","html_url":"https://github.com/Automattic/mongoose/issues/2864","id":67692536,"number":2864,"title":"Mongoose 3.8.26 gives bizarre results when used with Q promises","user":{"login":"alavers","id":2432382,"avatar_url":"https://avatars.githubusercontent.com/u/2432382?v=3","gravatar_id":"","url":"https://api.github.com/users/alavers","html_url":"https://github.com/alavers","followers_url":"https://api.github.com/users/alavers/followers","following_url":"https://api.github.com/users/alavers/following{/other_user}","gists_url":"https://api.github.com/users/alavers/gists{/gist_id}","starred_url":"https://api.github.com/users/alavers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alavers/subscriptions","organizations_url":"https://api.github.com/users/alavers/orgs","repos_url":"https://api.github.com/users/alavers/repos","events_url":"https://api.github.com/users/alavers/events{/privacy}","received_events_url":"https://api.github.com/users/alavers/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/37","html_url":"https://github.com/Automattic/mongoose/milestones/3.8.27","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/37/labels","id":1047806,"number":37,"title":"3.8.27","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":6,"closed_issues":4,"state":"open","created_at":"2015-03-31T22:06:53Z","updated_at":"2015-04-21T13:48:28Z","due_on":"2015-04-24T04:00:00Z","closed_at":null},"comments":2,"created_at":"2015-04-10T22:01:51Z","updated_at":"2015-04-21T18:59:58Z","closed_at":null,"body":"I use [Q.ninvoke](https://github.com/kriskowal/q) with mongoose and I've started seeing bizarre behavior after upgrading from 3.8.25 to 3.8.26. The following code snippet will reproduce the issue:\r\n\r\n```\r\nvar Q = require('q');\r\nvar mongoose = require('mongoose');\r\nvar Schema = mongoose.Schema;\r\n\r\nvar ArticleSchema = new Schema({\r\n    title: String,\r\n    tags: [String]\r\n});\r\n\r\nvar Article = mongoose.model('Article', ArticleSchema);\r\n\r\nreturn Q.ninvoke(Article, 'update', {\r\n    title: 'Hello'\r\n}, {\r\n    $push: {\r\n        tags: {\r\n            $each: ['nifty', 'awesome']\r\n        }\r\n    }\r\n}).then(function(res) {\r\n    return Q.ninvoke(Article, 'findOne', {\r\n        title: 'Hello'\r\n    });\r\n}).then(function(article) {\r\n    console.log(article);\r\n});\r\n```\r\n\r\nSupposing we with a document `{title:'Hello', tags:[]}`, after running the above code the tags some how get double inserted to give this:  `{title:'Hello', tags:['nifty', 'awesome', 'nifty', 'awesome']}`\r\n\r\nThis code works as expected in 3.8.25.\r\nAlso, this issue does not repro if I invoke `mongoose.update` directly, without using Q."},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/94905832","html_url":"https://github.com/Automattic/mongoose/issues/2864#issuecomment-94905832","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/2864","id":94905832,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"created_at":"2015-04-21T18:59:58Z","updated_at":"2015-04-21T18:59:58Z","body":"Well this is a wonky bug, courtesy of https://github.com/aheckmann/mquery/commit/3cca43758f1e9905f79ce368dd88240f30665311 . mquery >= 1.1 has a `.then()` function, so Q thinks that the return value of `Article.findOne()` is a promise. That function should have been introduced in 2.0 to mark it as backwards breaking in hindsight. This is an unintentional side effect of the fix for #1934, which bumped mquery version for 3.8.26. As you probably noticed, the below works:\r\n\r\n```javascript\r\nreturn Article.update({\r\n    title: 'Hello'\r\n}, {\r\n    $push: {\r\n        tags: {\r\n            $each: ['nifty', 'awesome']\r\n        }\r\n    }\r\n}).then(function(res) {\r\n    return Article.findOne({\r\n        title: 'Hello'\r\n    });\r\n}).then(function(article) {\r\n    console.log(article);\r\n});\r\n```\r\n\r\nAll solutions to this are roughly equally unpleasing. The easiest solution will probably be just deleting `mquery.prototype.then` in mongoose, but that's a bit of a dirty hack. Will try and see what happens."}}