Avoid duplicate entries on Mongoose array

后端 未结 2 1375
深忆病人
深忆病人 2020-12-31 09:59

I have a many to many relation using mongoose, that looks like this.

TeamSchema = new Schema 
    name : String
    players: [{ type: ObjectId, ref: \'Player         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 10:31

    Just use addToSet method:

    team.players.addToSet(player)
    team.save()
    

提交回复
热议问题