Before I begin, I want to state that no other answers regarding similar questions applied to this one. First consider the following JSON code
"_id": "1412302013-01-20,11:32:22"
"display": [
{
"Name": "LOG-11-05-SH-O1.mp4",
"Type": "Startup",
"Count": 2,
"Detail": [
{
"Start": "2013-01-20,11:32:22",
"End": "2013-01-20,11:32:30"
},
{
"Start": "2013-01-20,11:32:22",
"End": "2013-01-20,11:32:30"
}
]
},
{
"Name": "PUR-12-47-SH-X3.mp4",
"Type": "Movie",
"Count": "2",
"Detail": [
{
"Start": "2013-01-20,11:32:22",
"End": "2013-01-20,11:32:30"
},
{
"Start": "2013-01-20,11:32:22",
"End": "2013-01-20,11:32:30"
}
]
},
The ID is basically a combination of a license plate number concatted with a time stamp.
What I am trying to do is aggregate the sum for "Count", where the "Name": "LOG-11-05-SH-O1.mp4".
I could not even get simple sums to be aggregated, so trying to aggregate this sum has been impossible. When running my script in NodeJS, I either get undefined or [object Object].
I have no clue what is going wrong as I've been following various online examples.
After trying to use parvin's answer below, I am still getting undefined:
collection.aggregate([
{$match : {"display.Name" : "LOG-11-05-SH-O1.mp4"}},
{$unwind : "$display"},
{$group : {_id : "$_id",
name : {$first : "$display.Name"},
total : {$sum : "$display.Count"}
}
}], function(err, result) {
console.log("Aggregation: " + result.total);
});
I've been trying to format the code like these docs.