setDefaultsOnInsert not working with upsert setDefaultsOnInsert not working with upsert mongoose mongoose

setDefaultsOnInsert not working with upsert


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.