Docs Home → Develop Applications → MongoDB Manual
db.collection.distinct()![](/zh-cn/docs/v5.3/assets/link.svg)
On this page
Definition![](/zh-cn/docs/v5.3/assets/link.svg)
db.collection.distinct(field, query, options)
Important
mongosh Method
This is a
mongosh
method. This is not the documentation forNode.js
or other programming language specific driver methods.In most cases,
mongosh
methods work the same way as the legacymongo
shell methods. However, some legacy methods are unavailable inmongosh
.For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Finds the distinct values for a specified field across a single collection or view and returns the results in an array.
ParameterTypeDescriptionfield
stringThe field for which to return distinct values.query
documentA query that specifies the documents from which to retrieve the distinct values.options
documentOptional. A document that specifies the options. See Options.The
db.collection.distinct()
method provides a wrapper around thedistinct
command.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
$group
operator, as described in Retrieve Distinct Values with the Aggregation Pipeline.
The following diagram shows an example
db.collection.distinct()
call.
Options![](/zh-cn/docs/v5.3/assets/link.svg)
{ collation: <document> }
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. |
Behavior![](/zh-cn/docs/v5.3/assets/link.svg)
In a sharded cluster, the distinct
command may return
orphaned documents.
Array Fields![](/zh-cn/docs/v5.3/assets/link.svg)
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![](/zh-cn/docs/v5.3/assets/link.svg)
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.
Transactions![](/zh-cn/docs/v5.3/assets/link.svg)
To perform a distinct operation within a transaction:
For unsharded collections, you can use the
db.collection.distinct()
method/thedistinct
command as well as the aggregation pipeline with the$group
stage.For sharded collections, you cannot use the
db.collection.distinct()
method or thedistinct
command.To find the distinct values for a sharded collection, use the aggregation pipeline with the
$group
stage instead. See Distinct Operation for details.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
Client Disconnection![](/zh-cn/docs/v5.3/assets/link.svg)
Starting in MongoDB 4.2, if the client that issued db.collection.distinct()
disconnects before the operation completes, MongoDB marks db.collection.distinct()
for termination using killOp
.
Replica Set Member State Restriction![](/zh-cn/docs/v5.3/assets/link.svg)
Starting in MongoDB 4.4, to run on a replica set member,
distinct
operations require the member to be in
PRIMARY
or SECONDARY
state. If the member
is in another state, such as STARTUP2
, the
operation errors.
In previous versions, the operations also run when the member
is in STARTUP2
. The operations wait until the member
transitioned to RECOVERING
.
Examples![](/zh-cn/docs/v5.3/assets/link.svg)
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![](/zh-cn/docs/v5.3/assets/link.svg)
The following example returns the distinct values for the field
dept
from all documents in the inventory
collection:
db.inventory.distinct( "dept" )
The method returns the following array of distinct dept
values:
[ "A", "B" ]
Return Distinct Values for an Embedded Field![](/zh-cn/docs/v5.3/assets/link.svg)
The following example returns the distinct values for the field
sku
, embedded in the item
field, from all documents in the
inventory
collection:
db.inventory.distinct( "item.sku" )
The method returns the following array of distinct sku
values:
[ "111", "222", "333" ]
Tip
See also:
Dot Notation for information on accessing fields within embedded documents
Return Distinct Values for an Array Field![](/zh-cn/docs/v5.3/assets/link.svg)
The following example returns the distinct values for the field
sizes
from all documents in the inventory
collection:
db.inventory.distinct( "sizes" )
The method returns the following array of distinct sizes
values:
[ "M", "S", "L" ]
For information on distinct()
and array
fields, see the Behavior section.
Specify Query with distinct
![](/zh-cn/docs/v5.3/assets/link.svg)
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.inventory.distinct( "item.sku", { dept: "A" } )
The method returns the following array of distinct sku
values:
[ "111", "333" ]
Specify a Collation![](/zh-cn/docs/v5.3/assets/link.svg)
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" }
The following aggregation operation includes the Collation option:
db.myColl.distinct( "category", {}, { collation: { locale: "fr", strength: 1 } } )
For descriptions on the collation fields, see Collation Document.