use distinct and count in mongodb query code example Example: mongodb count distinct db.collection.distinct('field').length