Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findOneAndUpdate with {upsert: true} does not return document #2939

Closed
dfarr opened this issue Apr 29, 2015 · 4 comments
Closed

findOneAndUpdate with {upsert: true} does not return document #2939

dfarr opened this issue Apr 29, 2015 · 4 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@dfarr
Copy link

dfarr commented Apr 29, 2015

Recently upgraded to mongoose 4.0.2. The following code was working previously, but now results in the document being created but not passed to the callback:

Milestone.findOneAndUpdate({
    ......
}, {
    ......
}, {upsert: true}, function(err, res) {
    // err === null
    // res === null
    done(err, res);
});

This is only occurring on upsert. I can verify the document is created, but both err and res are consistently null. If I call the function again (now that the document exists) it works as expected.

mongoose: 4.0.2
node: v0.12.2
mongodb: 2.6.9

@vkarpov15
Copy link
Collaborator

Use the new: true option, see 4.0 release notes notes on #2262.

Milestone.findOneAndUpdate({
    ......
}, {
    ......
}, {upsert: true, 'new': true}, function(err, res) {
    // err === null
    // res === null
    done(err, res);
});

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Apr 29, 2015
@dfarr
Copy link
Author

dfarr commented Apr 29, 2015

Thanks! Why does the document still get created if I leave out new: true?

@vkarpov15
Copy link
Collaborator

new is false by default, and when new is false MongoDB returns the document as it was before the update operation was applied. In the case of upserts, there was no document, hence null.

@dfarr
Copy link
Author

dfarr commented Apr 29, 2015

Ah I see, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants