Skip to content

Example for findOneAndUpdate is confusing #14670

Closed
@davidnewcomb

Description

@davidnewcomb

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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:

const Character = mongoose.model('Character', new mongoose.Schema({
  name: String,
  age: Number
}));

await Character.create({ name: 'Jean-Luc Picard' });

const filter = { name: 'Jean-Luc Picard' };
const update = { age: 59 };

// `doc` is the document _before_ `update` was applied
let doc = await Character.findOneAndUpdate(filter, update);
doc.name; // 'Jean-Luc Picard'
doc.age; // undefined

doc = await Character.findOne(filter);   <-----
doc.age; // 59                           <-----

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?

doc = await Character.findOne(filter);
doc.age; // 59
doc.name; // 'Jean-Luc Picard'

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions