- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.distinct()
db.collection.distinct()¶
On this page
Definition¶
-
db.collection.distinct(field, query, options)¶ Finds the distinct values for a specified field across a single collection or view and returns the results in an array.
Parameter Type Description fieldstring The field for which to return distinct values. querydocument A query that specifies the documents from which to retrieve the distinct values. optionsdocument Optional. A document that specifies the options. See Options. The
db.collection.distinct()method provides a wrapper around thedistinctcommand. Results must not be larger than the maximum BSON size.
Options¶
| Field | Type | Description |
|---|---|---|
collation |
document | 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: When specifying collation, the If the collation is unspecified but the collection has a
default collation (see 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. |
Behavior¶
Array Fields¶
If the value of the specified field is an array,
db.collection.distinct() considers each element of the array
as a separate value.
For instance, if a field has as its value [ 1, [1], 1 ], then
db.collection.distinct() considers 1, [1], and 1 as separate values.
For an example, see Return Distinct Values for an Array Field.
Index Use¶
When possible, db.collection.distinct() operations can use indexes.
Indexes can also cover
db.collection.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:
Return Distinct Values for a Field¶
The following example returns the distinct values for the field
dept from all documents in the inventory collection:
The method returns the following array of distinct dept values:
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:
The method returns the following array of distinct sku values:
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:
The method returns the following array of distinct sizes values:
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":
The method returns the following array of distinct sku values:
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:
The following aggregation operation includes the Collation option:
For descriptions on the collation fields, see Collation Document.