I've created a model with a set matches
in it. However every time I update a document, it just adds the match to matches
, which results in duplicates. I'm wondering whether there is a method that lets me specify a key, in my case the matchId
, and if it already exists in the matches
set it should update that particular object in the set?
Model
var leagueSchema = new Schema({
_id: Number,
name: String,
league: Number,
matches: [{
matchId: Number,
date: Date,
tournament: String,
homeName: String,
awayName: String,
awayScore: Number,
homeScore: Number,
homeLogo: String,
awayLogo: String
}]
});
Update Query
var queryData = {'matchId': matchId, 'date': date, 'tournament': tournament, 'homeName': homeTeam, 'awayName': awayTeam, 'awayScore': awayScore, 'homeScore': homeScore, 'homeLogo': homeLogo, 'awayLogo': awayLogo};
League.update({league: leagueId}, {$addToSet: {matches: queryData}})
.exec(function(err, update) {
});