Definition定义
db.collection.distinct(field, query, options)Finds the distinct values for a specified field across a single collection or view and returns the unique results in an array.在单个集合或视图中查找指定字段的不重复值,并在数组中返回唯一结果。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
This method takes the following parameters:此方法采用以下参数:
field | ||
query | ||
options |
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运算符检索不重复值,如使用聚合管道检索不重复值中所述。
The following diagram shows an example 下图显示了db.collection.distinct() call.db.collection.distinct()调用的示例。
Options选项
{ collation: <document> }
collation |
|
Behavior行为
In a sharded cluster, the 在分片集群中,distinct command may return orphaned documents.distinct命令可能会返回孤立的文档。
For time series collections, the 对于时间序列集合,distinct command can't make efficient use of indexes. distinct命令无法有效利用索引。Instead, use a 相反,使用$group aggregation to group documents by distinct values. $group聚合按非重复的值对文档进行分组。For details, see Time Series Limitations.有关详细信息,请参阅时间序列限制。
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.field的值是一个数组,db.collection.distinct()会将数组的每个元素视为一个单独的值。
For instance, if a field has as its value 例如,如果一个字段的值为[ 1, [1], 1 ], then db.collection.distinct() considers 1, [1], and 1 as separate values.[ 1, [1], 1 ],则db.collection.distinct()将1、[1]和1视为单独的值。
For an example, see Return Distinct Values for an Array Field.例如,请参阅返回数组字段的不重复值。
Index Use索引使用
When possible, 如果可能,db.collection.distinct() operations can use indexes.db.collection.distinct()操作可以使用索引。
Indexes can also cover 索引还可以涵盖db.collection.distinct() operations. db.collection.distinct()操作。See Run Covered Queries for more information on queries covered by indexes.有关索引覆盖的查询的更多信息,请参阅运行覆盖的查询。
Transactions事务
To perform a distinct operation within a transaction:要在事务中执行distinct操作,请执行以下操作:
For unsharded collections, you can use the对于未记录的集合,您可以使用db.collection.distinct()method/thedistinctcommand as well as the aggregation pipeline with the$groupstage.db.collection.distinct()方法/distinct命令以及$group阶段的聚合管道。For sharded collections, you cannot use the对于分片集合,不能使用db.collection.distinct()method or thedistinctcommand.db.collection.distinct()方法或distinct命令。To find the distinct values for a sharded collection, use the aggregation pipeline with the要查找分片集合的不重复值,请使用带有$groupstage instead.$group阶段的聚合管道。See Distinct Operation for details.详见Distinct操作。
Important
In most cases, a distributed transaction incurs a greater performance cost over single document writes, and the availability of distributed 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 distributed transactions.也就是说,对于许多场景,适当地对数据进行建模将最大限度地减少对分布式事务的需求。
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和oplog大小限制),另请参阅生产注意事项。
Client Disconnection客户端断开连接
If the client that issued 如果发出db.collection.distinct() disconnects before the operation completes, MongoDB marks db.collection.distinct() for termination using killOp.db.collection.distinct()的客户端在操作完成之前断开连接,MongoDB将使用killOp标记db.collection.distinct()以终止。
Replica Set Member State Restriction副本集成员状态限制
To run on a replica set member, 要在副本集成员上运行,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,则操作错误。
Query Settings查询设置
New in version 8.0.在版本8.0中新增。
You can use query settings to set index hints, set operation rejection filters, and other fields. 您可以使用查询设置来设置索引提示、设置操作拒绝筛选器和其他字段。The settings apply to the query shape on the entire cluster. The cluster retains the settings after shutdown.这些设置适用于整个集群上的查询形状。集群在关闭后保留设置。
The query optimizer uses the query settings as an additional input during query planning, which affects the plan selected to run the query. You can also use query settings to block a query shape.查询优化器在查询规划期间使用查询设置作为额外输入,这会影响选择运行查询的计划。您还可以使用查询设置来阻止查询形状。
To add query settings and explore examples, see 要添加查询设置并探索示例,请参阅setQuerySettings.setQuerySettings。
You can add query settings for 您可以为find, distinct, and aggregate commands.find、distinct和aggregate命令添加查询设置。
Query settings have more functionality and are preferred over deprecated index filters.查询设置具有更多功能,并且优于已弃用的索引筛选器。
To remove query settings, use 要删除查询设置,请使用removeQuerySettings. To obtain the query settings, use a $querySettings stage in an aggregation pipeline.removeQuerySettings。要获取查询设置,请在聚合管道中使用$querySettings阶段。
Examples示例
The examples use the 示例使用包含以下文档的inventory collection that contains the following documents:inventory集合:
db.inventory.insertMany( [
{ _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.inventory.distinct( "dept" )
The method returns the following array of unique 该方法返回以下唯一dept values:dept值数组:
[ "A", "B" ]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集合中所有文档中嵌入在item字段中的字段sku的不重复值:
db.inventory.distinct( "item.sku" )
The method returns the following array of unique 该方法返回以下唯一sku values:sku值数组:
[ "111", "222", "333" ]
Tip
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集合中所有文档的字段大小的不重复值:
db.inventory.distinct( "sizes" )
The method returns the following array of unique 该方法返回以下唯一sizes values:sizes值数组:
[ "M", "S", "L" ]
For information on 有关distinct() and array fields, see the Behavior section.distinct()和数组字段的信息,请参阅行为部分。
Specify Query with distinct用distinct指定查询
distinctThe 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.inventory.distinct( "item.sku", { dept: "A" } )
The method returns the following array of unique 该方法返回以下唯一sku values:sku值数组:
[ "111", "333" ]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集合有以下文件:
db.myColl.insertMany( [
{ _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.有关排序规则字段的说明,请参阅排序规则文档。