user4105489
user4105489

Reputation:

MongoDB Aggregation after Find?

I'm new to MongoDB and trying to perform an Aggregation Operation on the result that I get after running the Find Operation.

I'm running the Find Operation to get the States, City & Population result within the 5 miles distance as follows:

db.zipcodes.find({loc : { $near : [-86, 54], $maxDistance: 5*(1/69) }})

Now, I want to perform an aggregation i.e. group the results by states and return the total population of group states in sorted fashion. I was trying something like this:

db.zipcodes.aggregate( { $group : { ... }, { db.zipcodes.find(...) }}, {$sort : {_population : 1}})

But unfortunately it is not working.

Any help would be appreciated.

Upvotes: 2

Views: 2955

Answers (1)

Clement Amarnath
Clement Amarnath

Reputation: 5466

Find query results cannot be passed on to aggregate, instead of find you can use $match and then the group operations

sample query

db.zipcodes.aggregate([{$match:{loc : { $near : [-86, 54], $maxDistance: 5*(1/69) }}},{$group : { ... }}, {$sort : {_population : 1}}])

Upvotes: 5

Related Questions