Mongoose findOneAndUpdate gives back the document from before the update
findOneAndUpdate resolves with the old values, but the database holds the new onesShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
The database has the new value, the returned object has the old oneThe default is to return the document before the updateFix - 2
Code 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
The field is unchanged in both the returned document and the databaseA field was set to undefined, so Mongoose dropped it from the updateFix
- 1
What does the error look like?
findOneAndUpdate resolves with the old values, but the database holds the new onesThe returned document does not show the field you just changedA 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 src2Fix
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); // 593Verify
The returned object now shows the new value.
const doc = await User.findOneAndUpdate({ _id: id }, { age: 60 }, { returnDocument: 'after' });
console.log(doc.age); // 60Confirmed by mongoosejs.com
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 src2Fix
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 srcConfirmed by mongoosejs.com
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
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix