I have an users collection
const userSchema = mongoose.Schema({
fullName: { type: String },
favClubsBeforeDeactivation: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Club' }],
})
I’m trying to update some user’s favClubsBeforeDeactivation
with some object ids using the followin query:
User.findOneAndUpdate(
{ _id: 'userObjectId' },
{ $set: { favClubsBeforeDeactivation: ['firstObjectId', 'secondObjectId'] } }
)
When the favClubsBeforeDeactivation
array exists already on that particular document, everything works fine but when the array does not exist yet, favClubsBeforeDeactivation
is added to the document as an empty array (I want it to also contain the ids).
Do you have any idea about how this can be fixed?