You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constmongoose=require('mongoose')const{ Schema }=mongoose;(asyncfunction(){console.log('connecting');varclient=awaitmongoose.connect(`mongodb://localhost:27017/`,{user: process.env.DBUSER,pass: process.env.DBPASS,});console.log('connected');constschema=newSchema({my_field: Number,default_field: {type:String,default: 'default'}},{collection: `test`,//disable versioning alltogetherversionKey: false});schema.index({my_field:1});constModel=mongoose.model(`Test`,schema);//Make sure it is cleanawaitModel.deleteMany({});//create a docawaitModel.create({my_field:1,default_field:'NOT DEFAULT'});//get the doc based on a filterconstfilter={my_field:1};letdocs=awaitModel.find(filter);console.log(`Doc with a non-default`,docs);awaitModel.findOneAndUpdate(filter,{},{upsert:true});docs=awaitModel.find(filter);console.log(`Docs with a duplicate`,docs);console.log('done');process.exit();})();
Prerequisites
Mongoose version
7.5.3
Node.js version
16.14.0
MongoDB server version
4.0 (documentDB)
Typescript version (if applicable)
No response
Description
When using findOneAndUpdate with upsert, the defaults of the schema is being applied to the filter.
The below steps to reproduce shows the problem. Inside castUpdate.js (
mongoose/lib/helpers/query/castUpdate.js
Line 129 in 1f64495
Thus when later on applying defaults, the reference to filter makes it update both the $setOnInsert in the update AND the filter.
This caused the filter in findOneAndUpdate to NOT match the expected docs and thus create a new one.
The bug has been detected in 7.5.3, but the code suggests that this is still an issue in 8.0.0 (https://github.com/Automattic/mongoose/blob/e15940051f868f56c0dc9cf935e42b31703bb18b/lib/helpers/query/castUpdate.js#L129C3-L129C37).
A workaround exists with setting the $setOnInsert for ALL the default fields to the defaults your self, to avoid them being set in the filter.
Steps to Reproduce
Expected Behavior
That the filter is NOT changed, and thus that the reproduction code would find the existing doc and not create a new one with a default.
The text was updated successfully, but these errors were encountered: