I have the following sample json, it doesn't seem to be too hard. Basically I'm trying to group all objects by the algorithm field, and then calculate the average from the $difference.close field.
The following is a sample object
{
"_id" : ObjectId("56b0c702c783c42f504f006c"),
"symbol" : "ALSK",
"algorithm" : "AvgForecaster",
"date" : "2016-02-01",
"real_quote" : {
"trade_date" : "2016-02-01",
"open" : "1.6",
"close" : "1.59",
"low" : "1.57",
"high" : "1.66",
"volume" : "65600",
"symbol" : "ALSK",
"adjusted_close" : "1.59"
},
"forecasted_quote" : {
"trade_date" : "2016-02-01",
"open" : "0.0",
"close" : "1.92",
"low" : "0.0",
"high" : "0.0",
"volume" : "0",
"symbol" : "ALSK",
"adjusted_close" : "0.0"
},
"difference" : {
"trade_date" : "2016-02-01",
"open" : "1.6",
"close" : "-0.33",
"low" : "1.57",
"high" : "1.66",
"volume" : "65600",
"symbol" : "ALSK",
"adjusted_close" : "1.59"
}
}
Then the query is as follows:
db.getCollection('Accuchecks').aggregate([
{
$group: {
_id: "$algorithm",
accuracy: {$avg: "$difference.close"}
}
}])
And then I get the following result:
{
"result" : [
{
"_id" : "DeltaForecaster",
"accuracy" : 0.0000000000000000
},
{
"_id" : "AvgForecaster",
"accuracy" : 0.0000000000000000
}
],
"ok" : 1.0000000000000000
}
Could the query is not picking the $difference.close field? In the full collection that value almost never 0.
Thanks