question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findOneAndUpdate option "new" has different default value from MongoDB findAndModify

See original GitHub issue

In Mongoose doc http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate

new: bool - true to return the modified document rather than the original. defaults to true

In MongoDB doc http://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/

new Boolean Optional. When true, returns the modified document rather than the original. The findAndModify() method ignores the new option for remove operations. The default is false.

The mongoose default value of options.new is true while MongoDB is false. This is really counter-intuitive.

Any reason for this API design?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
mnort9commented, Dec 13, 2015

This change makes the api less expressive IMO and will likely trip up newcomers. I believe most people will expect ‘findOneAndUpdate’ to return the updated doc. I can understand the desire to mirror the MongoDB driver, but creating an expressive high level api should take precedence.

Our dev team recently upgraded mongoose and we’ve had to go back to add what seems to be a boilerplate condition ({new: true}) to all of our ‘findOneAndUpdate’ queries. This seems like a step backward.

I could be missing something, but what scenario would you not want the updated doc to be returned?

0reactions
dreadrockseancommented, Apr 5, 2018

There are better ways to implement an optimistic query than this. It is very counter intuitive and time wasting for new comers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose default w/ findOneAndUpdate - Stack Overflow
I have the following schema: var UserSchema = new Schema({ username: { type: String, ...
Read more >
db.collection.findOneAndUpdate() — MongoDB Manual
Creates a new document if no documents match the filter . For more details see upsert behavior. Returns null after inserting the new...
Read more >
How to Use `findOneAndUpdate()` in Mongoose
By default, findOneAndUpdate() returns the document as it was before update was applied. const Character = mongoose.model('Character', new mongoose.
Read more >
MongoDB - db.collection.findOneAndUpdate() Method
When we update the document, the value of the _id field remains unchanged. This method will return the original document but if we...
Read more >
How to Use Mongoose's findOneAndUpdate Function
By default, findOneAndUpdate() returns the document as it was ... after MongoDB applied the given update , you need to set the new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found