Hi,
I have a collection that contains multiple documents, like this:
[{
"id" : 1,
"name": "test",
"phase": "2",
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 }
]
},{
"id" : 2,
"name": "test",
"phase": "2",
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 91, "mean" : 85, "std" : 4 }
]
}]
Is there a way to return one document with the array (grades) aggregated from all documents? as following:
[{
"id" : 1,
"name": "test",
"phase": "2",
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 }
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 91, "mean" : 85, "std" : 4 }
]
}]
The returned document has the shared attributes between all documents.
I used unwind and replaceRoot, as following:
db.collection.aggregate([{
$unwind: "$grades"
},{
$match: {
"grades.grade": {
$gte: 90
}
}
},{
$replaceRoot: {
newRoot: "$grades"
}
}])
but it returned the array of grades only, I need to put also the document information in the result in addition to the array that holds all elements of arrays from all documents.
Thanks