Phani
Phani

Reputation: 3325

Count of the result of a MongoDB aggregate query

After I write a MongoDB aggregate command, I would like to quickly look at the number of results it returned.

Say, my query is something like this: (probably more complex)

db.zipcodes.aggregate( [{ $group :
                            { _id : "$state" } }])

Then, right now I do the following to get a total count:

 db.zipcodes.aggregate( [{ $group :
                            { _id : "$state" }},
                         {'$group': {_id: null, count: {$sum: 1}}}])

Is there another quicker way to get a count of the results of a MongoDB aggregate query?

PS: I've just seen a related question: MongoDB Aggregation: How to get total records count? . Has there been any progress regarding this?

Upvotes: 1

Views: 3468

Answers (2)

Neha Sinha
Neha Sinha

Reputation: 171

db.zipcodes.aggregate( [{ $group :
                        { _id : "$state" } },
{
  $count: "total" 
} 
])

Upvotes: 1

Phani
Phani

Reputation: 3325

One workaround I found so far is to use a MongoDB GUI such as Robomongo. It reports the size of the Result.

enter image description here

Upvotes: 0

Related Questions