Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setDefaultsOnInsert not working with upsert

I am using typegoose to create a schema, where I am defining the default field for a property. This means that if I don't set a value for that field in the setQuery, it should use that default value in the schema while inserting.

This does not work with updateOne method where I set options as {upsert: true, setDefaultsOnInsert: true}

While, if I use create, the defaults are set correctly.

Mongoose: 5.7.14 MongoDB: 3.2.20 Node: 8 Typescript: 3.8.3 Typegoose: 5.9.1

like image 807
Gitesh Khanna Avatar asked May 09 '26 10:05

Gitesh Khanna


1 Answers

The reason why it wasn't working was because I was explicitly mentioning the field value as undefined .

Eg.

const updatePayload = {
id: "123",
dob: someObject.dateOfBirth
}

someObject.dateOfBirth was resulting to undefined. The defaults would have worked if I didn't have the dob key in the update payload.

like image 177
Gitesh Khanna Avatar answered May 12 '26 06:05

Gitesh Khanna