Heres what my mongoose schema looks like-
const GameLogSchema = new Schema({
_score: Number,
_playerData: {
x: Number,
y: Number,
},
_zoneData: [{ _x: Number, _height: Number }],
_pipeData: [{ _x: Number, _randomeHeightTop: Number }],
_gap: Number,
});
const PlayerSchema = new Schema({
/* other fields */
_gameLogs: {
type: [[GameLogSchema]],
},
});
This is what the data its supposed to deal with looks like -
Spreading one of those objects -
How to push an array of objects having arrays, into an array ? preferably in nodejs mongoose.
EDIT -
I tried to do something similar to whats given in Mongodb $push in nested array
I tried this with my schema -
const PlayerSchema = new Schema({
/* other fields */
_gameLogs: [
{
_logs: [{ _log: { _score: Number } }],
},
],
});
And heres my update function -
Player.findOneAndUpdate(
{ \* filter *\ },
{
$push: {
/* pushing something other, that works */
_gameLogs: {
'_gameLogs.$._logs': {
'_logs.$._log': { '_log.$._score': req.body.gameLogs.score },
},
},
},
$inc: { _totalGamesPlayed: 1, '_gameStreak._gamesPlayed': 1 },
},
{ safe: true, upsert: true, new: true, minimize: false },
(err, result) => {
console.log(result);
}
);
It outputs a list of { _logs: [], _id: 5f4f5979fba2d03c40d4aed7 },
among other things.