-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Example for findOneAndUpdate is confusing #14670
Comments
And other projects/people have copied the same omission, https://masteringjs.io/tutorials/mongoose/findoneandupdate#getting-started - only they like Star Wars better ;) |
How does the following example look? const Character = mongoose.model('Character', new mongoose.Schema({
name: String,
age: Number
}));
const _id = new mongoose.Types.ObjectId('0'.repeat(24));
let doc = await Character.create({ _id, name: 'Jean-Luc Picard' });
doc; // { name: 'Jean-Luc Picard', _id: ObjectId('000000000000000000000000') }
const filter = { name: 'Jean-Luc Picard' };
const update = { age: 59 };
// The result of `findOneAndUpdate()` is the document _before_ `update` was applied
doc = await Character.findOneAndUpdate(filter, update);
doc; // { name: 'Jean-Luc Picard', _id: ObjectId('000000000000000000000000') } The example shows that you have replaced the document and lost the name field. Can you change it to show all the data you get back. It updates and doesn't replace right? No, |
docs(findoneandupdate): improve example that shows findOneAndUpdate() returning doc before updates were applied
Excellent, looks great to me. Thanks for cleaning that up. |
Prerequisites
Mongoose version
8.4.1
Node.js version
22.2.0
MongoDB server version
7.0.11
Typescript version (if applicable)
No response
Description
On page: https://mongoosejs.com/docs/tutorials/findoneandupdate.html
Referring to example:
The example shows that you have replaced the document and lost the name field. Can you change it to show all the data you get back. It updates and doesn't replace right?
Steps to Reproduce
Go to the web page: https://mongoosejs.com/docs/tutorials/findoneandupdate.html
Expected Behavior
Both examples of
doc
should show the same values.The text was updated successfully, but these errors were encountered: