On this page本页内容
distinct
¶Finds the distinct values for a specified field across a single collection. 在单个集合中查找指定字段的不同值。distinct
returns a document that contains an array of the distinct values. distinct
返回一个包含不同值数组的文档。The return document also contains an embedded document with query statistics and the query plan.退货文档还包含一个带有查询统计信息和查询计划的嵌入式文档。
The command takes the following form该命令采用以下形式
The command contains the following fields:该命令包含以下字段:
distinct |
string | |
key |
string | |
query |
document | |
readConcern |
document |
|
collation |
document | Optional.
|
comment |
any |
|
Note
Results must not be larger than the maximum BSON size. 结果不得大于最大BSON大小。If your results exceed the maximum BSON size, use the aggregation pipeline to retrieve distinct values using the 如果结果超过最大BSON大小,请使用聚合管道使用$group
operator, as described in Retrieve Distinct Values with the Aggregation Pipeline.$group
运算符检索不同的值,如使用聚合管道检索不同的值中所述。
MongoDB also provides the shell wrapper method MongoDB还提供了用于db.collection.distinct()
for the distinct
command. distinct
命令的shell包装器方法db.collection.distinct()
。Additionally, many MongoDB drivers provide a wrapper method. 此外,许多MongoDB驱动程序提供了包装器方法。Refer to the specific driver documentation.请参阅特定的驱动程序文档。
In a sharded cluster, the 在分片集群中,distinct
command may return orphaned documents.distinct
命令可能会返回孤立文档。
If the value of the specified 如果指定field
is an array, distinct
considers each element of the array as a separate value.field
的值是数组,distinct
会将数组中的每个元素视为单独的值。
For instance, if a field has as its value 例如,如果一个字段的值[ 1, [1], 1 ]
, then distinct
considers 1
, [1]
, and 1
as separate values.[ 1, [1], 1 ]
,则distinct
将1
、[1]
和1
视为单独的值。
For an example, see Return Distinct Values for an Array Field.有关示例,请参阅针对数组字段返回不同值。
When possible, 如果可能,distinct
operations can use indexes.distinct
操作可以使用索引。
Indexes can also cover 索引还可以覆盖distinct
operations. distinct
的操作。See Covered Query for more information on queries covered by indexes.有关索引覆盖的查询的更多信息,请参阅覆盖的查询。
To perform a distinct operation within a transaction:要在事务中执行不同的操作:
db.collection.distinct()
method/the distinct
command as well as the aggregation pipeline with the $group
stage.db.collection.distinct()
方法/distinct
命令以及带有$group
阶段的聚合管道。db.collection.distinct()
method or the distinct
command.db.collection.distinct()
方法也不能使用distinct
命令。
To find the distinct values for a sharded collection, use the aggregation pipeline with the 要查找分片集合的不同值,请将聚合管道与$group
stage instead. $group
阶段一起使用。See Distinct Operation for details.有关详细信息,请参阅Distinct操作。
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.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出distinct
disconnects before the operation completes, MongoDB marks the distinct
for termination (i.e. killOp
on the operation).distinct
的客户机在操作完成之前断开连接,MongoDB会将distinct
标记为终止(即操作上killOp
)。
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,distinct
operations require the member to be in PRIMARY
or SECONDARY
state. distinct
操作要求该成员处于PRIMARY
或SECONDARY
状态。If the member is in another state, such as 如果成员处于另一种状态,例如STARTUP2
, the operation errors.STARTUP2
,则操作会出错。
In previous versions, the operations can also be run when the member is in 在以前的版本中,当成员在STARTUP2
. STARTUP2
中时,也可以运行这些操作。However, the operations wait until the member transitions to 但是,这些操作将等待到成员转换到 RECOVERING
.RECOVERING
。
The examples use the 这些示例使用包含以下文档的inventory
collection that contains the following documents:inventory
集合:
The following example returns the distinct values for the field 以下示例从dept
from all documents in the inventory
collection:inventory
集合中的所有文档返回字段dept
的不同值:
The command returns a document with a field named 该命令返回一个名为values
that contains the distinct dept
values:values
的文档,其中包含不同的dept
值:
The following example returns the distinct values for the field 下面的示例返回sku
, embedded in the item
field, from all documents in the inventory
collectioninventory
集合中所有文档中嵌入在item
字段中的字段sku
的不同值:
The command returns a document with a field named 该命令返回一个名为values
that contains the distinct sku
values:values
的文档,该字段包含不同的sku
值:
See also参阅
Dot Notation for information on accessing fields within embedded documents用于访问嵌入文档中字段的信息的点符号
The following example returns the distinct values for the field 以下示例返回sizes
from all documents in the inventory
collection:inventory
集合中所有文档的字段sizes
的不同值:
The command returns a document with a field named 该命令返回一个名为values
that contains the distinct sizes
values:values
的文档,该字段包含不同的sizes
值:
For information on 有关distinct
and array fields, see the Behavior section.distinct
和array
字段的信息,请参阅行为部分。
distinct
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"
:dept
等于"A"
的文档中返回item
字段中嵌入的字段sku
的不同值:
The command returns a document with a field named 该命令返回一个名为values
that contains the distinct sku
values:values
的文档,该字段包含不同的sku
值:
New in version 3.4.版本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:myColl
集合包含以下文档:
The following aggregation operation includes the Collation option:以下聚合操作包括排序规则选项:
For descriptions on the collation fields, see Collation Document.有关排序规则字段的说明,请参阅排序规则文档。
To override the default read concern level of 要覆盖"local"
, use the readConcern
option."local"
的默认读取关注级别,请使用readConcern
选项。
The following operation on a replica set specifies a Read Concern of 以下对副本集的操作指定"majority"
to read the most recent copy of the data confirmed as having been written to a majority of the nodes."majority"
的读取关注点,以读取确认已写入多数Node的数据的最新副本。
Note
"majority"
, replica sets must use WiredTiger storage engine."majority"
的读关注级别,副本集必须使用WiredTiger存储引擎。
You can disable read concern 对于具有三成员主从仲裁器(PSA)体系结构的部署,可以禁用读关注点"majority"
for a deployment with a three-member primary-secondary-arbiter (PSA) architecture; however, this has implications for change streams (in MongoDB 4.0 and earlier only) and transactions on sharded clusters. "majority"
;然而,这对变更流(仅在MongoDB 4.0及更早版本中)和分片集群上的事务有影响。For more information, see Disable Read Concern Majority.有关更多信息,请参阅禁用读取关注多数。
To ensure that a single thread can read its own writes, use 要确保单个线程可以读取自己的写入,请对副本集的主线程使用"majority"
read concern and "majority"
write concern against the primary of the replica set."majority"
读取关注点和"majority"
写入关注点。