0

I've got the following query::

db.collection.aggregate({'$group':{
  '_id': "$city"
}})

which give the following results:

"result" : [ 
    {
        "_id" : "city1"

    }, 
    {
        "_id" : "city2"

    }, 
    {
        "_id" : "city3"

    }, 
    {
        "_id" : "city4"

    }
],
"ok" : 1

There are four grouped cities, how can I calculate number of fields which are grouped? Like this:

"result" : [ 
    {
        "count_grouped_fields" : 4

    }, 

1 Answer 1

1

You can add another $group pipeline:

{$group:{'_id': "city_count", count : { $sum : 1 }}}

Or use the distinct command (no aggregation) :

db.collection.distinct("city")

Sign up to request clarification or add additional context in comments.

Comments

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.