distinct
On this page本页内容
Definition定义
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.返回一个包含剔除重复值数组的文档。The return document also contains an embedded document with query statistics and the query plan.返回文档还包含一个带有查询统计信息和查询计划的嵌入文档。TipIn在mongosh
, this command can also be run through thedb.collection.distinct()
helper method..mongosh
中,这个命令也可以通过db.collection.distinct()
助手方法运行。。Helper methods are convenient for助手方法对mongosh
users, but they may not return the same level of information as database commands.mongosh
用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要方便,或者需要额外的返回字段,请使用数据库命令。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
distinct | string | |
key | string | |
query | document | |
readConcern | document | readConcern 选项具有以下语法:readConcern: { level: <value> }
|
collation | document | collation collation 选项具有以下语法:collation: { locale field is mandatory; all other collation fields are optional. locale 字段是必需的;所有其他排序规则字段都是可选的。db.createCollection() ), the operation uses the collation specified for the collection.db.createCollection() ),则操作将使用为集合指定的排序规则。 |
comment | any |
|
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. Additionally, many MongoDB drivers provide a wrapper method. Refer to the specific driver documentation.distinct
命令提供了shell包装方法db.collection.distinct()
。此外,许多MongoDB驱动程序提供了一个包装器方法。请参阅具体的驱动程序文档。
Behavior行为
In a sharded cluster, the 在分片集群中,distinct
command may return orphaned documents.distinct
命令可能会返回孤立文档。
Array Fields数组字段
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
视为单独的值。
Starting in MongoDB 6.0, the 从MongoDB 6.0开始,distinct
command returns the same results for collections and views when using arrays.distinct
命令在使用数组时为集合和视图返回相同的结果。
For examples, see:有关示例,请参阅:
Index Use索引使用
When possible, 如果可能,distinct
operations can use indexes.distinct
操作可以使用索引。
Indexes can also cover 索引还可以涵盖distinct
operations. See Covered Query for more information on queries covered by indexes.distinct
的操作。有关索引所涵盖查询的更多信息,请参阅覆盖查询。
Transactions事务
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
stagedb.collection.distinct()
方法/distinct
命令以及带有$group
阶段的聚合管道.For sharded collections, you cannot use the对于分片集合,不能使用db.collection.distinct()
method or thedistinct
command.db.collection.distinct()
方法或distinct
命令。To find the distinct values for a sharded collection, use the aggregation pipeline with the要查找分片集合的不同值,请使用带有$group
stage instead. See Distinct Operation for details.$group
阶段的聚合管道。有关详细信息,请参阅剔除重复操作。
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客户端断开连接
Starting in MongoDB 4.2, if the client that issued 从MongoDB 4.2开始,如果在操作完成之前发出distinct
disconnects before the operation completes, MongoDB marks distinct
for termination using killOp
.distinct
的客户端断开连接,MongoDB会使用killOp
标记distinct
以终止。
Replica Set Member State Restriction副本集成员状态限制
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. If the member is in another state, such as STARTUP2
, the operation errors.distinct
操作需要该成员处于PRIMARY
或SECONDARY
状态。如果成员处于其他状态,例如STARTUP2
,则操作将出错。
In previous versions, the operations also run when the member is in 在以前的版本中,成员处于STARTUP2
. STARTUP2
时也会运行这些操作。The operations wait until the member transitioned to 操作将等待,直到成员转换到RECOVERING
.RECOVERING
。
Index Filters and Collations索引筛选器和排序规则
Starting in MongoDB 6.0, an index filter uses the collation previously set using the 从MongoDB 6.0开始,索引筛选器使用之前使用planCacheSetFilter
command.planCacheSetFilter
命令设置的排序规则。
Examples实例
The examples use the 示例使用包含以下文档的inventory
collection that contains the following documents:inventory
集合:
{ "_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:inventory
集合中所有文档中字段dept
的不同值:
db.runCommand ( { distinct: "inventory", key: "dept" } )
The command returns a document with a field named 该命令返回一个具有名为values
that contains the distinct dept
values:values
的字段的文档,该字段包含不同的dept
值:
{
"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:inventory
集合中所有文档中嵌入在物料字段中的字段sku
的不同值:
db.runCommand ( { distinct: "inventory", key: "item.sku" } )
The command returns a document with a field named 该命令返回一个具有名为values
that contains the distinct sku
values:values
的字段的文档,该字段包含不同的sku
值:
{
"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:inventory
集合中所有文档的字段sizes
的不同值:
db.runCommand ( { distinct: "inventory", key: "sizes" } )
The command returns a document with a field named 该命令返回一个文档,其中包含一个名为values
that contains the distinct sizes
values:values
的字段,该字段包含不同sizes
的值:
{
"values" : [ "M", "S", "L" ],
"ok" : 1
}
For information on 有关distinct
and array fields, see the Behavior section.distinct
字段和数组字段的信息,请参阅行为部分。
Arrays in Collections and Views集合和视图中的数组
Starting in MongoDB 6.0, the 从MongoDB 6.0开始,distinct
command returns the same results for collections and views when using arrays.distinct
命令在使用数组时为集合和视图返回相同的结果。
The following example creates a collection named 以下示例为每个文档创建一个名为sensor
with an array of temperature values for each document:sensor
的集合,其中包含一组温度值:
db.sensor.insertMany( [
{ _id: 0, temperatures: [ { value: 1 }, { value: 4 } ] },
{ _id: 1, temperatures: [ { value: 2 }, { value: 8 } ] },
{ _id: 2, temperatures: [ { value: 3 }, { value: 12 } ] },
{ _id: 3, temperatures: [ { value: 1 }, { value: 4 } ] }
] )
The following example creates a view named 以下示例从sensorView
from the sensor
collection:sensor
集合中创建一个名为sensorView
的视图:
db.createView( "sensorView", "sensor", [] )
The following example uses 以下示例使用distinct
to return the unique values from the temperatures
array in the sensor
collection:distinct
返回sensor
集合中temperatures
数组的唯一值:
db.sensor.distinct( "temperatures.1.value" )
The 1
in temperatures.1.value
specifies the temperatures
array index.temperatures.1.value
中的1
指定temperatures
数组索引。
Example output:示例输出:
[ 4, 8, 12 ]
Example for sensorView
:sensorView
示例:
db.sensorView.distinct( "temperatures.1.value" )
Example output:输出示例:
从MongoDB 6.0开始为[ 4, 8, 12 ]
starting in MongoDB 6.0 (identical to result returned from thesensor
collection).[ 4, 8, 12 ]
(与sensor
集合返回的结果相同)。在低于6.0的MongoDB版本中为[]
in MongoDB versions earlier than 6.0.[]
。
Specify Query with distinct
指定具有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
的不同值:
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
的字段的文档,该字段包含不同的sku
值:
{
"values" : [ "111", "333" ],
"ok" : 1
}
Specify a Collation指定排序规则
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
包含以下文档:
{ _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:以下聚合操作包括collation
选项:
db.runCommand(
{
distinct: "myColl",
key: "category",
collation: { locale: "fr", strength: 1 }
}
)
For descriptions on the collation fields, see Collation Document.有关排序规则字段的说明,请参阅排序规则文档。
Override Default Read Concern覆盖默认读取关注
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"
的读取关注,以读取已确认写入大多数节点的数据的最新副本。
Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.无论读取关注级别如何,节点上的最新数据都可能无法反映系统中数据的最新版本。
db.runCommand(
{
distinct: "restaurants",
key: "rating",
query: { cuisine: "italian" },
readConcern: { level: "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"
写入关注。