MongoDB - Aggregation Framework (Total Count)

Assaf Hershko

When running a normal "find" query on MongoDB I can get the total result count (regardless of limit) by running "count" on the returned cursor. So, even if I limit to result set to 10 (for example) I can still know that the total number of results was 53 (again, for example).

If I understand it correctly, the aggregation framework, however, doesn't return a cursor but simply the results. And so, if I used the $limit pipeline operator, how can I know the total number of results regardless of said limit?

I guess I could run the aggregation twice (once to count the results via $group, and once with $limit for the actual limited results), but this seems inefficient.

An alternative approach could be to attach the total number of results to the documents (via $group) prior to the $limit operation, but this also seems inefficient as this number will be attached to every document (instead of just returned once for the set).

Am I missing something here? Any ideas? Thanks!

For example, if this is the query:

db.article.aggregate(
    { $group : {
        _id : "$author",
        posts : { $sum : 1 }
    }},
    { $sort : { posts: -1 } },
    { $limit : 5 }
);

How would I know how many results are available (before $limit)? The result isn't a cursor, so I can't just run count on it.

Dylan Tong

Assaf, there's going to be some enhancements to the aggregation framework in the near future that may allow you to do your calculations in one pass easily, but right now, it is best to perform your calculations by running two queries in parallel: one to aggregate the #posts for your top authors, and another aggregation to calculate the total posts for all authors. Also, note that if all you need to do is a count on documents, using the count function is a very efficient way of performing the calculation. MongoDB caches counts within btree indexes allowing for very quick counts on queries.

If these aggregations turn out to be slow there are a couple of strategies. First off, keep in mind that you want start the query with a $match if applicable to reduce the result set. $matches can also be speed up by indexes. Secondly, you can perform these calculations as pre-aggregations. Instead of possible running these aggregations every time a user accesses some part of your app, have the aggregations run periodically in the background and store the aggregations in a collection that contains pre-aggregated values. This way, your pages can simply query the pre-calculated values from this collection.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Total count and field count with condition in a single MongoDB aggregation pipeline

Group into group and count using aggregation framework in MongoDB

Get cursor count in Mongodb with aggregation framework

Count item occurrences in an array with mongodb aggregation framework

grand total in MongoDB aggregation

MongoDB: return 0 for $count of certain values (aggregation framework)

Mongodb aggregation $unwind then count

Mongodb aggregation, group and then count

MongoDB Aggregation Query to Count

mongodb use $count in aggregation

MongoDB aggregation count

MongoDB aggregation framework with $and[$or[]]

Mongodb Explain for Aggregation framework

MongoDB aggregation framework match OR

MongoDB Orders/sales aggregation group Per Month Sum Total + Count Field

How to paginate aggregation query results in MongoDB and get a total document count (Node.js + Mongoose)?

How can i count total documents and also grouped counts simultanously in mongodb aggregation?

How to total Count in mongoDB?

MongoDb Aggregation $count to display 0

How to group and count with MongoDB aggregation

$count in mongodb under aggregation framkework

MongoDB Count() vs. Aggregation

Count of MongoDB aggregation match results

Double aggregation with distinct count in MongoDB

How to count fields in Mongodb Aggregation

Mongodb aggregation framework alternative for Cassandra

Is there a floor function in Mongodb aggregation framework?

Index optimization for mongodb aggregation framework

Array intersection in mongodb with Aggregation framework