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
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.