What I am trying to do I have a document with teamid as the unique identifier and name of the team. I have an array 'teamList' in my model which would hold the names of all the team members. When a new member is added, I only want names which are not existing to be added. So, if a name is already present it should not be added.
PROBLEM I would like to ensure that there are no repeats inside the teamList array. But right now, all objects in the names array are added to the teamList despite using $addToSet and turning off _id inside the array.
Here is my schema:
var teamSchema = new Schema({
teamid: {
type: String,
required: true
},
created: {
type: Date
},
lastUpdated: {
type: Date,
default: Date.now,
required: true
},
teamList: [{
name: {
type: String,
required: true,
unique: true
},
dateAdded: {
type: Date,
default: Date.now
},
_id:false
}]
});
module.exports = mongoose.model('teamModel', teamSchema);
This is my code:
var mongoose = require('mongoose');
teamModel = mongoose.model('teamModel');
var names = [{name:'Robert'},{name:'Alice'},{name:'Bob'}];
var teamName = 'ManU';
teamModel.update({
teamid: teamName
}, {
$addToSet: {
teamList: {
$each: names
}
}
}, {
upsert: true
},
function (err, data) {
console.log(data);
});
My question is similar to - http://blog.open-tribute.org/2015/05/09/NodeJS-Mongoose-addToSet-duplicates-on-objects/ , Avoid duplicate entries on Mongoose array
But $addToSet does not work for me probably because I am passing in an array to be added to the teamList array and so I would have to use $each (which is not covered in the previous questions)