3 Ways to Return Distinct Values in MongoDB

Distinct values are those values with redundant duplicates removed. In other words, distinct values are unique values.

In MongoDB there are a few ways we can return distinct values in a query. This article presents 3 ways to return distinct values in the mongo shell.

Sample Data

The examples on this page use the following collection called pets:

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
{ "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 }
{ "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }

The distinct() Method

In MongoDB, the db.collection.distinct() method finds the distinct values for a specified field across a single collection or view and returns the results in an array.

Example:

db.pets.distinct( "type" )

Result:

[ "Bat", "Cat", "Dog" ]

These three values are the distinct values for the type field in the above documents.

The pets part simply names the collection for which to return the distinct values from.

The db.collection.distinct() method is a shell wrapper method for the distinct command (below).

The distinct Command

In MongoDB, the distinct aggregation command finds the distinct values for a specified field across a single collection.

It returns a document that contains an array of the distinct values, as well as an embedded document with query statistics and the query plan.

db.runCommand ( { distinct: "pets", key: "type" } )

Result:

{ "values" : [ "Bat", "Cat", "Dog" ], "ok" : 1 }

We can see that the same distinct values are returned, except this time they’re in a document that also contains the other info.

The $group Aggregation Operator

Depending on your needs, you can also use the $group aggregation operator to return distinct values.

Example:

db.pets.aggregate( [ { $group : { _id : "$type" } } ] )

Result:

{ "_id" : "Cat" }
{ "_id" : "Bat" }
{ "_id" : "Dog" }

In this case the distinct values are returned in separate documents.

This approach could be useful if you expect your results to be large. If your results are larger than the maximum BSON size (16 megabytes at the time of writing) you may need to use this approach.