I have records in a collection of the following format.
STUDENT
[
{
"name" : "student A",
"type" : 1,
"results" : [
{
"position" : 1,
"percent" : 90,
"test_id" : ObjectId("aaaa")
},
{
"position" : 2,
"percent" : 88,
"test_id" : ObjectId("bbbb")
}
]
},
{
"name" : "student B",
"type" : 1,
"results" : [
{
"position" : 2,
"percent" : 56,
"test_id" : ObjectId("bbbb")
}
]
}
]
TEST:
{
"_id" : ObjectId("aaaa"),
"name" : "Test A",
},
{
"_id" : ObjectId("bbbb"),
"name" : "Test B",
}
This is my required output, Condition: Test.name = "Test A"
[
{
"name" : "student A",
"type" : 1,
"results" : [
{
"position" : 1,
"percent" : 90,
"test" : {
"_id" : ObjectId("aaaa"),
"name" : "Test A",
}
},
{
"position" : 2,
"percent" : 88,
"test" : {
"_id" : ObjectId("bbbb"),
"name" : "Test B",
}
}
]
}
]
I've tried various combinations of aggregate, unwind and project but still can't quite get there and would really appreciate any suggestions.


Test.name = "student A"