MongoDB distinct()

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.

Distinct values are those with redundant duplicates removed. Distinct values are unique values. For example, if you have 2 or 3 documents with the same value, the distinct command will return just one value.

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

Example

Suppose we have a collection called pets with the following documents.

{ "_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 }

We can use the distinct() method to return the distinct pet types.

db.pets.distinct( "type" )

Result:

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

So even though there are four dogs and two cats, the array only contains one of each. The distinct() method removed the duplicate values.

The original document has only one bat and so the distinct() method doesn’t change that – there were no duplicate values to dedupe.

Embedded Documents

You can use dot notation to get distinct values from an embedded field

Suppose we have a collection called products that contains the following documents:

{ "_id" : 1, "product" : { "name" : "Shirt", "color" : "White" }, "sizes" : [ "S", "M", "L" ] }
{ "_id" : 2, "product" : { "name" : "Shirt", "color" : "Green" }, "sizes" : [ "S", "M", "XL" ] }
{ "_id" : 3, "product" : { "name" : "Shirt", "color" : "White" }, "sizes" : [ "S", "M", "L" ] }
{ "_id" : 4, "product" : { "name" : "Shorts", "color" : "Green" }, "sizes" : [ "M", "XS" ] }
{ "_id" : 5, "product" : { "name" : "Shorts", "color" : "Brown" }, "sizes" : [ "S", "M" ] }
{ "_id" : 6, "product" : { "name" : "Cap", "color" : "Purple" }, "sizes" : [ "M" ] }
{ "_id" : 7, "product" : { "name" : "Shoes", "color" : "Brown" }, "sizes" : [ "S", "M", "L" ] }
{ "_id" : 8, "product" : { "name" : "Shirt", "color" : "White" }, "sizes" : [ "M", "L", "XL" ] }
{ "_id" : 9, "product" : { "name" : "Cap", "color" : "Green" }, "sizes" : [ "M", "L" ] }

We can use the following query to return distinct values for the product names.

db.products.distinct(
    "product.name"
)

Result:

[ "Cap", "Shirt", "Shoes", "Shorts" ]

We could do the same thing for the color field.

db.products.distinct(
    "product.color"
)

Result:

[ "Brown", "Green", "Purple", "White" ]

Get Distinct Values from an Array

Here’s how to use the distinct() method to get the distinct values from the above array.

db.products.distinct( "sizes" )

Result:

[ "L", "M", "S", "XL", "XS" ]

Use distinct() with a Query

You can provide a query to specify the documents from which to retrieve the distinct values. To do this, add the query after the field.

Example:

db.products.distinct( "product.name", { sizes: "S" } )

Result:

[ "Shirt", "Shoes", "Shorts" ]

More Information

The db.collection.distinct() method also accepts a collation parameter, which allows you to specify language-specific rules for string comparison, such as rules for letter case and accent marks.

See the MongoDB documentation for more information.