How does mongo count one field according to multiple values of another?

  mongodb, question

In the database

{ "_id" : ObjectId("5a66ef690fe318d00bbaf106"), "sex" : "man", "name" : "a" }
 { "_id" : ObjectId("5a66ef780fe318d00bbaf107"), "sex" : "man", "name" : "b" }
 { "_id" : ObjectId("5a66ef7d0fe318d00bbaf108"), "sex" : "man", "name" : "c" }
 { "_id" : ObjectId("5a66ef820fe318d00bbaf109"), "sex" : "man", "name" : "d" }
 { "_id" : ObjectId("5a66ef8c0fe318d00bbaf10a"), "sex" : "woman", "name" : "e" }
 { "_id" : ObjectId("5a66ef8e0fe318d00bbaf10b"), "sex" : "woman", "name" : "f" }
 { "_id" : ObjectId("5a66ef910fe318d00bbaf10c"), "sex" : "woman", "name" : "g" }
 { "_id" : ObjectId("5a66ef930fe318d00bbaf10d"), "sex" : "woman", "name" : "h" }
 { "_id" : ObjectId("5a66ef950fe318d00bbaf10e"), "sex" : "woman", "name" : "i" }
 { "_id" : ObjectId("5a66ef970fe318d00bbaf10f"), "sex" : "woman", "name" : "j" }
 { "_id" : ObjectId("5a66efab0fe318d00bbaf110"), "sex" : "no_know", "name" : "z" }
 { "_id" : ObjectId("5a66efae0fe318d00bbaf111"), "sex" : "no_know", "name" : "x" }
 { "_id" : ObjectId("5a66efb00fe318d00bbaf112"), "sex" : "no_know", "name" : "c" }

Expected results
===>

{"sex": "man", "sum": 4},
 {"sex": "woman", "sum": 6} // do not count sex === no_know

It seems to be using group by. I have been searching for a long time and haven’t found a corresponding one.

He went through the documents carefully again. I took care of it myself.
Specific ideas: use aggregation

  1. Match corresponding data
  2. Group that match data
  3. Statistics
db.b.aggregate([
 {$match:
 {"sex": {$in: ["man","woman"]}}
 //match match sex is man and woman.
 {$group:
 {
 _id: "$sex", // according to what field grouping
 Count: {$sum:1} // count other fields and return count: ***
 }
 }
 ])