terms and get the count aggregation in mongodb code example
Example: mongodb aggregate count
#return the number of documents whose "field" field has a value of A
query = db.collection.aggregate([{
"$match": {
"field": "A"
}
},{
"$count": "number of occurrences"
}])