I have a label schema:
var labelSchema = mongoose.Schema({
name: String,
desc: String,
postIds: [mongoose.Schema.ObjectId],
removed: { type: Boolean, default: false }
})
I want to update postIds array when a label is assigned to a post, so I do in my update post request:
Label.update({'_id': { $in: post.labelIds }}, {$addToSet: {postIds:req.body.id}}, {multi: true})
Where post.labelIds is an array of label ids. I've tested the query with find() method and it works fine, it finds all the labels. However when I check the database for updated records, no label is updated.
When I do a console.log of post.LabelIds before the update command, it returns:
[ 'uniqueidhere', 'anotheruniqidthere' ]
and req.body.id
uniqueidhere
So the ids are there, but somehow the update doesn't work.