Reputation: 910
I have the following query which is supposed to be getting, for each state, the amount of flights that fly within the state. Basically, the origin state and the destination state are the same. Pretty straightforward.
function(){
return db.flight_info.aggregate([
{ $project: { matches: { $eq:[ '$ORIGIN_STATE_ABR', '$DEST_STATE_ABR' ] }} },
{ $match: { matches:true } },
{ $group: { _id: "$ORIGIN_STATE_ABR", total: {$sum: 1} } }
]);
}
The issue is that this is not actually grouping my data for some reason. The output simply looks like this:
{
"_id": null,
"total": 61402
}
while it should be something along the lines of:
{
{"_id": "FL",
"total": 620 },
{"_id": "GA",
"total": 896},
...
}
Upvotes: 0
Views: 34
Reputation: 13079
You're missing the ORIGIN_STATE_ABR in your $project and that's causing an issue in your grouping.
db.flight_info.aggregate([
{ $project: { ORIGIN_STATE_ABR: 1,
matches: { $eq:[ '$ORIGIN_STATE_ABR', '$DEST_STATE_ABR' ] } } },
{ $match: { matches:true } },
{ $group: { _id: "$ORIGIN_STATE_ABR", total: { $sum: 1 } } }
]);
Upvotes: 1