Site icon Satish Gandham

Get the number of documents in a MongoDB collection with unique/distinct values for a field

Some times we need to get the number of documents with a distinct/unique value. An example use case is in the classic blog example.

You can use the following commands to get the answers for the above questions

db.posts.distinct('author').length
db.posts.distinct('tag').length

Reference:

Exit mobile version