Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mongoose findOneAndUpdate gives back the document from before the update

ErrorfindOneAndUpdate resolves with the old values, but the database holds the new ones

Short answer

findOneAndUpdate returns the document as it was before the update unless you ask otherwise. Pass returnDocument: 'after' in the options. The older new: true and returnOriginal: false still appear in examples but are deprecated. If a field still looks unchanged, check that you did not set it to undefined, because Mongoose strips undefined keys from updates.

  • Applies to Mongoose 8, Node.js 22, MongoDB 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the returned document and then read the same record again. If the second read shows the new value, the update worked and only the return value is old.

    const returned = await User.findOneAndUpdate({ _id: id }, { age: 59 });
    const stored = await User.findById(id);
    console.log(returned.age, stored.age);
  2. 2Match what you see, then jump to its fix

    1. 1The database has the new value, the returned object has the old oneThe default is to return the document before the updateFix
    2. 2Code copied from older examples passes new: false, returnOriginal: true, or an option name that is spelled differentlyThe deprecated new and returnOriginal options are used or mixed upFix
    3. 3The field is unchanged in both the returned document and the databaseA field was set to undefined, so Mongoose dropped it from the updateFix

What does the error look like?

  • findOneAndUpdate resolves with the old values, but the database holds the new ones
  • The returned document does not show the field you just changed
  • A field you meant to clear is still set after the update

Cause 1 of 3

The default is to return the document before the update

1Check

Look at the third argument of the call. If it has no returnDocument option (and no new or returnOriginal), the default applies, which is the pre-update document.

grep -rn "findOneAndUpdate\|findByIdAndUpdate" --include=*.js --include=*.ts src

2Fix

Add returnDocument: 'after' to the options. Use 'before' only when you really want the old copy, for example to log what changed.

const doc = await User.findOneAndUpdate(
  { _id: id },
  { age: 59 },
  { returnDocument: 'after' }
);
console.log(doc.age); // 59

3Verify

The returned object now shows the new value.

const doc = await User.findOneAndUpdate({ _id: id }, { age: 60 }, { returnDocument: 'after' });
console.log(doc.age); // 60

Confirmed by mongoosejs.com

Did this fix it?

Cause 2 of 3

The deprecated new and returnOriginal options are used or mixed up

1Check

Search for the old option names. The Mongoose guide says new and returnOriginal are deprecated in favour of returnDocument, and maps new: true and returnOriginal: false to 'after'.

grep -rn "new: \|returnOriginal" --include=*.js --include=*.ts src

2Fix

Replace them with returnDocument so the intent is clear in one place.

// before
{ new: true }
{ returnOriginal: false }
// after
{ returnDocument: 'after' }

3Verify

Run the same call and confirm the returned value matches what the database holds.

grep -rn "returnDocument" --include=*.js --include=*.ts src

Confirmed by mongoosejs.com

Did this fix it?

Cause 3 of 3

A field was set to undefined, so Mongoose dropped it from the update

1Check

Log the update object before the call. In Mongoose 6 and later, keys with an undefined value are always removed from the update.

console.log(JSON.stringify(update), Object.keys(update));

2Fix

Send null to clear a field, or send a real value. Do not rely on undefined.

await User.findOneAndUpdate(
  { _id: id },
  { $set: { nickname: null } },
  { returnDocument: 'after' }
);

3Verify

Read the record again and check that the field now shows null.

const stored = await User.findById(id); console.log(stored.nickname);

Confirmed by mongoosejs.com

Did this fix it?

How do I stop it coming back?

  • Always pass returnDocument explicitly so the behaviour is visible in code review.
  • Wrap findOneAndUpdate in one helper in your data layer so the option lives in a single place.
  • Validate update objects before the call so an undefined field is caught early.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.