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 using findAndModify #4315

Closed
adamreisnz opened this issue Jul 10, 2016 · 3 comments
Closed

findOneAndUpdate using findAndModify #4315

adamreisnz opened this issue Jul 10, 2016 · 3 comments
Milestone

Comments

@adamreisnz
Copy link
Contributor

adamreisnz commented Jul 10, 2016

Why is the findOneAndUpdate method using findAndModify under the hood instead of findOneAndUpdate?

I need to pass projection settings but it's not working because findAndModifiy doesn't take such parameters, and as such it's returning the entire document.

E.g.:

Product
      .findOneAndUpdate({
        _id: productId,
      }, {
        $push: {colors: data},
      }, {
        projection: {
          name: 1, 'colors.$': 1,
        },
        returnNewDocument: true,
      })
@vkarpov15
Copy link
Collaborator

Because mongoose's findOneAndUpdate() long predates the existence of findOneAndUpdate() in the underlying mongodb driver, and the driver's findOneAndUpdate() is just a thin wrapper around findAndModify anyway. Also, the driver's findOneAndUpdate() has several quirky semantics, especially related to the new option and the difference between new and returnNewDocument, that are troublesome for mongoose.

Projections not working is a bit of a problem. Try using 'fields' instead of 'projection', because that's the name the underlying findAndModify function uses

@vkarpov15 vkarpov15 added this to the 4.5.7 milestone Jul 10, 2016
@adamreisnz
Copy link
Contributor Author

Thanks for clarifying. Yes I noticed that there were some odd differences in naming, e.g. fields vs projection and new vs. returnNewDocument.

@vkarpov15
Copy link
Collaborator

Yeah there's a little confusion there because every API wants to be it's own special snowflake :) Now that the mongodb driver added a findOneAndUpdate() function that has its own quirks, not quite sure yet with how to proceed on mongoose's fineOneAndUpdate()

vkarpov15 added a commit that referenced this issue Jul 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants