On this page
distinct
On this page
Definition
distinct-
Finds the distinct values for a specified field across a single collection.
distinctreturns a document that contains an array of the distinct values. The return document also contains an embedded document with query statistics and the query plan.The command takes the following form
{ distinct: "<collection>", key: "<field>", query: <query>, readConcern: <read concern document>, collation: <collation document> }The command contains the following fields:
Field Type Description distinctstring The name of the collection to query for distinct values. keystring The field for which to return distinct values. querydocument Optional. A query that specifies the documents from which to retrieve the distinct values. readConcerndocument Optional. Specifies the read concern.
The readConcern option has the following syntax:
Changed in version 3.6.
readConcern: { level: <value> }Possible read concern levels are:
"local". This is the default read concern level."available". This is the default for reads against secondaries when Read Operations and Causally Consistent Sessions and “level” are unspecified. The query returns the instance’s most recent data."majority". Available for replica sets that use WiredTiger storage engine."linearizable". Available for read operations on theprimaryonly.
For more formation on the read concern levels, see Read Concern Levels.
For
"local"(default) or"majority"read concern level, you can specify theafterClusterTimeoption to have the read operation return data that meets the level requirement and the specified after cluster time requirement. For more information, see Read Operations and Causally Consistent Sessions.collationdocument Optional.
Specifies the collation to use for the operation.
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.
The collation option has the following syntax:
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }When specifying collation, the
localefield is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.If the collation is unspecified but the collection has a default collation (see
db.createCollection()), the operation uses the collation specified for the collection.If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons.
You cannot specify multiple collations for an operation. For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort.
New in version 3.4.
Note
Results must not be larger than the maximum BSON size. If your results exceed the maximum BSON size, use the aggregation pipeline to retrieve distinct values using the
$groupoperator, as described in Retrieve Distinct Values with the Aggregation Pipeline.MongoDB also provides the shell wrapper method
db.collection.distinct()for thedistinctcommand. Additionally, many MongoDB drivers provide a wrapper method. Refer to the specific driver documentation.
Behavior
In a sharded cluster, the distinct command may return orphaned documents.
Array Fields
If the value of the specified field is an array, distinct considers each element of the array as a separate value.
For instance, if a field has as its value [ 1, [1], 1 ], then distinct considers 1, [1], and 1 as separate values.
For an example, see Return Distinct Values for an Array Field.
Index Use
When possible, distinct operations can use indexes.
Indexes can also cover distinct operations. See Covered Query for more information on queries covered by indexes.
Examples
The examples use the inventory collection that contains the following documents:
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
Return Distinct Values for a Field
The following example returns the distinct values for the field dept from all documents in the inventory collection:
db.runCommand ( { distinct: "inventory", key: "dept" } )
The command returns a document with a field named values that contains the distinct dept values:
{
"values" : [ "A", "B" ],
"ok" : 1
}
Return Distinct Values for an Embedded Field
The following example returns the distinct values for the field sku, embedded in the item field, from all documents in the inventory collection:
db.runCommand ( { distinct: "inventory", key: "item.sku" } )
The command returns a document with a field named values that contains the distinct sku values:
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
See also
Dot Notation for information on accessing fields within embedded documents
Return Distinct Values for an Array Field
The following example returns the distinct values for the field sizes from all documents in the inventory collection:
db.runCommand ( { distinct: "inventory", key: "sizes" } )
The command returns a document with a field named values that contains the distinct sizes values:
{
"values" : [ "M", "S", "L" ],
"ok" : 1
}
For information on distinct and array fields, see the Behavior section.
Specify Query with distinct
The following example returns the distinct values for the field sku, embedded in the item field, from the documents whose dept is equal to "A":
db.runCommand ( { distinct: "inventory", key: "item.sku", query: { dept: "A"} } )
The command returns a document with a field named values that contains the distinct sku values:
{
"values" : [ "111", "333" ],
"ok" : 1
}
Specify a Collation
New in version 3.4.
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.
A collection myColl has the following documents:
{ _id: 1, category: "café", status: "A" }
{ _id: 2, category: "cafe", status: "a" }
{ _id: 3, category: "cafE", status: "a" }