count unique documents based on field - mongodb code example Example: mongodb count distinct db.collection.distinct('field').length