2

So I have this code:

db.collection.find( { sex: 'Male' } ).count()

and what I want to do is also count sex: 'Female' at the same time instead of

db.collection.find( { sex: 'Male' } ).count()
db.collection.find( { sex: 'Female' } ).count()

Is there a way to do it in a single query? Because I am generating a pie chart based on their length. Any help would be much appreciated.

1

1 Answer 1

9
db.collection.aggregate([
    {"$group" : {_id:"$sex", count:{$sum:1}}}
])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.