findOneAndUpdate option "new" has different default value from MongoDB findAndModify
See original GitHub issueIn 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:
- Created 9 years ago
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
There are better ways to implement an optimistic query than this. It is very counter intuitive and time wasting for new comers.