I have a many to many relation using mongoose, that looks like this.
TeamSchema = new Schema
name : String
players: [{ type: ObjectId, ref: \'Player
Use the $addToSet update operator like so:
Team.update({_id: team._id}, {$addToSet: {players: player}})
Assuming player
is the ObjectId of a player, it will only be added to the team's players
array if it's not already present.
Just use addToSet method:
team.players.addToSet(player)
team.save()