I have a question about updating nested documents in MongoDB. I was using Mongoose but because I couldn't manage to find the answer, so I switched to my Robo3T shell (v.1.1.1).
What I want to do is insert an answer to a question in a round. The document has an array of rounds. Each round has an array of questions. Each question has an array of answers. In a later stadium I also want to be able to update that answer, but I guess when I get this right, that won't be that difficult.
I have this answer that I want to insert:
{
answer: "Intelligent answer",
approved: false,
teamId: ObjectId("59faf27e40b719352a62e5f1"),
id: ObjectId("59faf27e40b719352a62e5f1")
}
I have this document structure:
{
"_id" : ObjectId("59faf27e40b719352a62e5f1"),
"name" : "AvengersQuizz",
"password" : "verysecure",
"status" : "Playing",
"rounds" : [
{
"_id" : 1,
"questions" : [
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d7"),
"_id" : ObjectId("59faf27e40b719352a62e623"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e625")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e624")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d5"),
"_id" : ObjectId("59faf27e40b719352a62e620"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e622")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e621")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d4"),
"_id" : ObjectId("59faf27e40b719352a62e61d"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e61f")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e61e")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d3"),
"_id" : ObjectId("59faf27e40b719352a62e61a"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e61c")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e61b")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d6"),
"_id" : ObjectId("59faf27e40b719352a62e617"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e619")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e618")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2db"),
"_id" : ObjectId("59faf27e40b719352a62e614"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e616")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e615")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2da"),
"_id" : ObjectId("59faf27e40b719352a62e611"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e613")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e612")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d9"),
"_id" : ObjectId("59faf27e40b719352a62e60e"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e610")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e60f")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d8"),
"_id" : ObjectId("59faf27e40b719352a62e60b"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e60d")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e60c")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2df"),
"_id" : ObjectId("59faf27e40b719352a62e608"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e60a")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e609")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2de"),
"_id" : ObjectId("59faf27e40b719352a62e605"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e607")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e606")
}
]
},
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2dd"),
"_id" : ObjectId("59faf27e40b719352a62e602"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e604")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e603")
}
]
}
],
"categories" : [
ObjectId("59faf27d40b719352a62e2cb"),
ObjectId("59faf27d40b719352a62e2cc"),
ObjectId("59faf27d40b719352a62e2cd")
]
},
{
"_id" : 2,
"questions" : [
{
"status" : "Closed",
"questionId" : ObjectId("59faf27d40b719352a62e2d7"),
"_id" : ObjectId("59faf27e40b719352a62e5ff"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e601")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e600")
}
]
},
{
"status" : "Open",
"questionId" : ObjectId("59faf27d40b719352a62e2d5"),
"_id" : ObjectId("59faf27e40b719352a62e5fc"),
"answers" : [
{
"answer" : "a",
"approved" : false,
"teamId" : ObjectId("59faf27c40b719352a62e2c8"),
"_id" : ObjectId("59faf27e40b719352a62e5fe")
},
{
"answer" : "ab",
"approved" : true,
"teamId" : ObjectId("59faf27c40b719352a62e2c9"),
"_id" : ObjectId("59faf27e40b719352a62e5fd")
}
]
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2d4"),
"_id" : ObjectId("59faf27e40b719352a62e5fb"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2d3"),
"_id" : ObjectId("59faf27e40b719352a62e5fa"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2d6"),
"_id" : ObjectId("59faf27e40b719352a62e5f9"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2db"),
"_id" : ObjectId("59faf27e40b719352a62e5f8"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2da"),
"_id" : ObjectId("59faf27e40b719352a62e5f7"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2d9"),
"_id" : ObjectId("59faf27e40b719352a62e5f6"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2d8"),
"_id" : ObjectId("59faf27e40b719352a62e5f5"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2df"),
"_id" : ObjectId("59faf27e40b719352a62e5f4"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2de"),
"_id" : ObjectId("59faf27e40b719352a62e5f3"),
"answers" : []
},
{
"status" : "Queued",
"questionId" : ObjectId("59faf27d40b719352a62e2dd"),
"_id" : ObjectId("59faf27e40b719352a62e5f2"),
"answers" : []
}
],
"categories" : [
ObjectId("59faf27d40b719352a62e2cb"),
ObjectId("59faf27d40b719352a62e2cc"),
ObjectId("59faf27d40b719352a62e2cd")
]
}
],
"teams" : [
ObjectId("59faf27c40b719352a62e2c8"),
ObjectId("59faf27c40b719352a62e2c9"),
ObjectId("59faf27c40b719352a62e2ca")
],
"__v" : 0
}
This is what I have tried to use:
db.getCollection('quizzes').update({
rounds: {
$elemMatch: {
_id: 2,
questions: {
$elemMatch: {
questionId: ObjectId("59faf27d40b719352a62e2d5")}}
}
}
},
{
$push: {
"rounds.$[rounds].questions.$[questions].answers": {
answer: "Intelligent answer",
approved: false,
teamId: ObjectId("59faf27e40b719352a62e5f1"),
id: ObjectId("59faf27e40b719352a62e5f1")
}
}
}, {
arrayFilters: [
{"rounds._id": 2},
{"questions._id": ObjectId("59faf27e40b719352a62e5fc")}]
})
It returns the following error:
cannot use the part (rounds of rounds.$[rounds].questions.$[questions].answers) to traverse the element ({rounds: [ { _id: 1, questions: [ { status: "Closed", questionId: ObjectId('59faf27d40b719352a62e2d7'), _id: ... (cut off because of space)
I see similar questions, but they do not answer my question fully. I have tried those answers but still cannot seem to make this work. Can anyone explain to me what I'm doing wrong?