I am using Node.js and Mongoose. player and tournament variables are Mongoose objects, fetched just before.
I want to add a new tournamentSession object (NOT a Mongoose object) into the tournamentSessions field of the player object. I am using the findOneAndUpdate to be able to make sure that I dont add the same tournement twice (using the "$ne")
Player.findOneAndUpdate({
'_id': player._id,
'tournamentSessions.tournament': {
'$ne': tournament._id
}
}, {
'$push': {
'tournamentSessions': {
'tournament': tournament._id,
'level': 1,
'status': 'LIMBO',
'score': 0,
}
}
}, function(err, updatedPlayer) {
// ...
});
Everything works fine, except that the "_id" field containing an ObjectID is added to the newly added session inside the tournamentSessions array, and I cant understand why this is happening.
If I manually add the "_id" field and with the value "BLABLABLA", the "_id" field is never stored (as it shouldnt, coz its not declared in the schema)
Ye, And here is the schemas:
var Player = mongoose.model('Player', Schema({
createdAt: { type: Date, default: Date.now },
lastActiveAt: Date,
clientVersion: String,
tournamentSessions: [{
tournament: { type: Schema.Types.ObjectId, ref: 'Tournament' },
level: Number,
status: String,
score: Number
}],
friends: Array
}));
var Tournament = mongoose.model('Tournament', Schema({
createdAt: { type: Date, default: Date.now },
open: Boolean,
number: Number,
level: Number,
reactionLimit: Number,
deadlineAt: Date,
stats: {
total: Number,
limbo: Number,
blessed: Number,
damned: Number
}
}));