0

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},
     ...
 }

1 Answer 1

1

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 } } }
]);
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Awesome :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.