2016-10-19

MongoDB - Count distinct values of a given field, given a query

This will retrieve the distinct values in a json string (docs object)"

db.COLLECTION_NAME.distinct( "field_to_count", {query});



This would simply retrieve the count of distinct items (not the entire object with the list of all distinct values):

db.COLLECTION_NAME.distinct( "field_to_count", {query}).length;



No comments:

Post a Comment