Push to array within subdocument in mongoose
The solution is below. Note this is using Q and Express. The part of note is the 'users.$.tags
. I thought I had tried this but I guess not! I also used $pushAll
instead, but $each
might also work. My tags
is always an array.
var tags = req.body.tags, contentId = mongoose.Types.ObjectId( req.body.id )console.log( tags )Content.update( { 'users._id': contentId }, { $pushAll: { 'users.$.tags': tags } }).exec().then( function ( result ) { console.log( result ) resolve( result )}, function ( error ) { if ( error ) return reject( error )})