db.collection.distinct()
On this page本页内容
Definition定义
db.collection.distinct(field, query, options)
- Important
mongosh
Method方法This page documents a本页记录了一个mongosh
method.mongosh
方法。This is not the documentation for database commands or language-specific drivers, such as Node.js.这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the有关数据库命令,请参阅distinct
command.distinct
命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。For the legacy对于遗留的mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:mongo
shell文档,请参阅相应MongoDB Server版本的文档:Finds the distinct values for a specified field across a single collection or view and returns the results in an array.在单个集合或视图中查找指定字段的不同值,并以数组形式返回结果。This method takes the following parameters:此方法采用以下参数:Parameter参数Type类型Description描述field
string The field for which to return distinct values.要为其返回不同值的字段。query
document A query that specifies the documents from which to retrieve the distinct values.指定要从中检索不同值的文档的查询。options
document Optional.可选的。A document that specifies the options.指定选项的文档。See Options.请参见选项。NoteResults 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 | document | collation: { locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.locale 字段是必需的;所有其他排序规则字段都是可选的。有关字段的说明,请参阅排序规则文档。db.createCollection() ), the operation uses the collation specified for the collection.db.createCollection() ),则操作将使用为集合指定的排序规则。 |
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, 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 Covered Query for more information on queries covered by indexes.有关索引所涵盖查询的更多信息,请参阅覆盖查询。
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
stage.db.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.$group
阶段的聚合管道。See Distinct Operation for details.有关详细信息,请参阅Distinct操作。
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开始,如果发出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副本集成员状态限制
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 also run when the member is in 在以前的版本中,成员处于STARTUP2
. STARTUP2
时也会运行这些操作。The operations wait until the member transitioned to 操作将等待,直到成员转换到RECOVERING
.RECOVERING
。
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.inventory.distinct( "dept" )
The method returns the following array of distinct 该方法返回以下不同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 distinct 该方法返回以下不同sku
values:sku
值的数组:
[ "111", "222", "333" ]
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.inventory.distinct( "sizes" )
The method returns the following array of distinct 该方法返回以下不同sizes
values:sizes
值的数组:
[ "M", "S", "L" ]
For information on 有关distinct()
and array fields, see the Behavior section.distinct()
和数组字段的信息,请参阅行为部分。
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.inventory.distinct( "item.sku", { dept: "A" } )
The method returns the following array of distinct 该方法返回以下不同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
包含以下文档:
{ _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.有关排序规则字段的说明,请参阅排序规则文档。