$documentNumber (aggregation)
On this page本页内容
Definition定义
New in version 5.0. 5.0版新增。
$documentNumber
Returns the position of a document (known as the document number) in the 返回文档在$setWindowFields
stage partition.$setWindowFields
阶段分区中的位置(称为文档编号)。
The $setWindowFields
stage sortBy field determines the document number. $setWindowFields
阶段sortBy
字段确定文档编号。For more information on how MongoDB compares fields with different types, see BSON comparison order.有关MongoDB如何比较不同类型的字段的更多信息,请参阅BSON比较顺序。
$documentNumber
returns a unique number for each document in a partition, even if multiple documents have identical sortBy field values in the partition.为分区中的每个文档返回一个唯一的数字,即使分区中有多个文档具有相同的sortBy
字段值。
$documentNumber
is only available in the 仅在$setWindowFields
stage.$setWindowFields
阶段中可用。
$documentNumber
syntax:语法:
{ $documentNumber: { } }
$documentNumber
does not accept any parameters.不接受任何参数。
Behavior行为
$documentNumber
includes documents that have a sortBy field that is 包括null
or missing.sortBy
字段为null
或缺失的文档。
$documentNumber
, $rank
, and $denseRank
return the position of the documents based on the sortBy field values.$documentNumber
、$rank
和$denseBank
根据sortBy
字段值返回文档的位置。
$documentNumber
differs from $rank
and $denseRank
in how documents with identical sortBy field values in a partition are treated:$documentNumber
与$rank
和$denseRank
的不同之处在于如何处理分区中具有相同sortBy
字段值的文档:
$rank
and和$denseRank
return the same position (known as the rank) for those documents.为这些文档返回相同的位置(称为级别)。$documentNumber
returns a unique position (known as the document number) for those documents.返回这些文档的唯一位置(称为文档编号)。
See the example in Document Number for Duplicate, Null, and Missing Values.请参阅文档编号中的重复值、Null
值和缺失值示例。
Examples实例
Document Number for Each State每个州的文件编号
Create a 创建一个包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
集合:
db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )
This example uses 此示例使用$documentNumber
in the $setWindowFields
stage to output the cake sales document number for each state
:$setWindowFields
阶段中的$documentNumber
来输出每个状态的蛋糕销售文档编号:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
documentNumberForState: {
$documentNumber: {}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
.state
对集合中的文档进行分区。There are partitions forCA
andWA
.CA
和WA
有分区。sortBy: { quantity: -1 }
sorts the documents in each partition by按quantity
in descending order (-1
), so the highestquantity
is first.quantity
降序(-1
)对每个分区中的文档进行排序,因此quantity
最高的是第一个。
output
sets the document number in a new field calleddocumentNumberForState
shown in the following results.output
在一个名为documentNumberForState
的新字段中设置文档编号,如以下结果所示。documentNumberForState
is unique within eachstate
partition.documentNumberForState
在每个state
分区中都是唯一的。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "documentNumberForState" : 1 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "documentNumberForState" : 2 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "documentNumberForState" : 3 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "documentNumberForState" : 1 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "documentNumberForState" : 2 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "documentNumberForState" : 3 }
Document Number for Duplicate, Null, and Missing Values重复值、Null
值和缺失值的文档编号
Create a 创建cakeSalesWithDuplicates
collection where:cakeSalesWithDuplicates
集合,其中:
Cake sales are placed in the state of California (蛋糕销售在加利福尼亚州(CA
) and Washington (WA
).CA
)和华盛顿州(WA
)。Documents 6 to 8 have the same文档6至文档8具有与文档5相同的quantity
andstate
as document 5.quantity
和state
。Document 9 has the same文档9具有与文档4相同的quantity
andstate
as document 4.quantity
和quantity
。Document 10 has a文档10的null
quantity
.quantity
为null
。Document 11 is missing the文档11缺少quantity
.quantity
。
db.cakeSalesWithDuplicates.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 },
{ _id: 6, type: "strawberry", orderDate: new Date("2020-01-08T06:12:03Z"),
state: "WA", price: 41, quantity: 134 },
{ _id: 7, type: "strawberry", orderDate: new Date("2020-01-01T06:12:03Z"),
state: "WA", price: 34, quantity: 134 },
{ _id: 8, type: "strawberry", orderDate: new Date("2020-01-02T06:12:03Z"),
state: "WA", price: 40, quantity: 134 },
{ _id: 9, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39, quantity: 162 },
{ _id: 10, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39, quantity: null },
{ _id: 11, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39 }
] )
This example uses 此示例使用$documentNumber
in the $setWindowFields
stage to output the cakeSalesWithDuplicates
document number for each state
:$setWindowFields
阶段中的$documentNumber
来输出每个state
的cakeSalesWithDuplicates
文档编号:
db.cakeSalesWithDuplicates.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
documentNumberForState: {
$documentNumber: {}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
.state
对集合中的文档进行分区。There are partitions forCA
andWA
.CA
和WA
有分区。sortBy: { quantity: -1 }
sorts the documents in each partition by按quantity
in descending order (-1
), so the highestquantity
is first.quantity
降序(-1
)对每个分区中的文档进行排序,因此quantity
最高的是第一个。
output
sets the document number in a new field called在名为documentNumberForState
shown in the following results.documentNumberForState
的新字段中设置文档编号,如以下结果所示。documentNumberForState
is unique within eachstate
partition, and there aredocumentNumberForState
values for documents withnull
quantity
and missingquantity
values.documentNumberForState
在每个state
分区中都是唯一的,quantity
值为null
和quantity
值缺失的文档都有documentNumberForState
值。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "documentNumberForState" : 1 }
{ "_id" : 9, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "quantity" : 162, "documentNumberForState" : 2 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "documentNumberForState" : 3 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "documentNumberForState" : 4 }
{ "_id" : 10, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "quantity" : null, "documentNumberForState" : 5 }
{ "_id" : 11, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "documentNumberForState" : 6 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "documentNumberForState" : 1 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "documentNumberForState" : 2 }
{ "_id" : 6, "type" : "strawberry", "orderDate" : ISODate("2020-01-08T06:12:03Z"),
"state" : "WA", "price" : 41, "quantity" : 134, "documentNumberForState" : 3 }
{ "_id" : 7, "type" : "strawberry", "orderDate" : ISODate("2020-01-01T06:12:03Z"),
"state" : "WA", "price" : 34, "quantity" : 134, "documentNumberForState" : 4 }
{ "_id" : 8, "type" : "strawberry", "orderDate" : ISODate("2020-01-02T06:12:03Z"),
"state" : "WA", "price" : 40, "quantity" : 134, "documentNumberForState" : 5 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "documentNumberForState" : 6 }