The Situation
I have a MongoDB of the following data structure
teamName: (String)
data: (Array)
---date: (Timestamp)
---userResults: (Object)
------userHash: (StringHash)
------score: (String)
------comment: (String)
Here is a short example on PasteBin.
The Question
Given a specific teamName and date (within the team data array), I'd like to append a new row ({userHash: '', score: '', comment: ''}) into userResults array.
How can I update an array within a given document, after querying the parent node, and the parents parent node?
What I've tried so far
I've tried findOneAndUpdate. It inserted an entire new document, right from the root though.
TeamRecordSchema.findOneAndUpdate(
{teamName: teamName, data: {date: today}},
{$set:{teamName: teamName, data: {date: today, userResults: [userResponse]}}},
{new: true, upsert: true},
function(err, savedDoc){
// Do stuff with err and savedDoc
});
I've tried save, and again it just creates an entire new document at the root level, within my db
const _userResponse = new TeamRecordSchema(teamUserResponse);
_userResponse.save((err, savedDoc) => {
// Do stuff with err and savedDoc accordingly
});
I've also tried several variations based on the documentation, but I either get the same results, or an error.
TeamRecordSchema.findOneAndUpdate( {teamName: teamName, 'data.date': today}}, {$push:{'data.$.userResults': {userHash: '', score: '', comment: ''}}}, {new: true, upsert: true}, function(err, savedDoc){ });