Count of the result of a MongoDB aggregate query
NickName:Phani Ask DateTime:2014-06-26T03:41:10

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?

Copyright Notice:Content Author:「Phani」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24417175/count-of-the-result-of-a-mongodb-aggregate-query

Answers
Neha Sinha 2022-03-17T12:49:40

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


More about “Count of the result of a MongoDB aggregate query” related questions

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(...

Show Detail

MongoDB query for aggregate function retuning empty result

Following is the query i had written to get the count based on event name. $data= DB::collection(Events::$collection)->raw(function($collection) { return $collection->aggregate([ ['$...

Show Detail

MongoDB aggregate query count

I'm making an application with MongoDB and C#. I use the MongoDB C# driver. I have the following collection: { _id: 5099803df3f4948bd2f98391, title: Toy Story, genres: [ "Animation", "...

Show Detail

Count with MongoDB aggregate $group result

I have a database query with pymongo , like so pipeline = [ {"$group": {"_id": "$product", "count": {"$sum": 1}}}, ] rows = list(collection_name.

Show Detail

mongoDb $in with aggregate query

How do I write an $in query along with aggregate in mongoDB? I want to write an equivalent mongoDB query for the SQL below SELECT name,count(something) from collection1 where name in (<<list...

Show Detail

MongoDB aggregate query to SpringDataMongoDB

I have below MongoDB aggregate query and would like to have it's equivalent SpringData Mongodb query. MongoDB Aggregate Query : db.response.aggregate( // Pipeline [ //

Show Detail

Convert Mongodb shell query with map and aggregate to php

I wrote a mongodb query that I am having a hard time converting to php code: var geoips = db.geoip.find().map(function(like){ return like.ip; }); var result = db.audit.aggregate([ { $match: {...

Show Detail

MongoDB Aggregate Query in Scala

I'm attempting to convert my MongoDB aggregate query: db.identification.aggregate([ { $match: { userId: "b0e644d2-5a0c-4048-abea-6ae17c337e57" } }, { $

Show Detail

Does MongoDB support aggregate queries on the result of the aggregate query?

I have an aggregate query that returns the count of records a property has. db.collection.aggregate([ { $group : { _id : "$propertyId", count: { $sum: 1 } ...

Show Detail

How to store mongodb aggregate query result in spring mongotemplate?

I am using an aggregate query in mongodb to find the sum of an attribute in all the documents present in a collection. Query: db.conversation.aggregate( [ { $match:{ $and:[{"mailBo...

Show Detail