“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$first(聚合累加器)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例
$first

5.0 版本中的更改

返回将表达式应用于一组文档中的第一个文档所产生的值。仅当文档按定义的顺序排列时才有意义。

$first 可在以下阶段使用:

注意

消歧

本页介绍$first聚合累加器。 有关$first数组操作符,请参阅$first (array operator)

$first 事务语法:

{ $first: <expression> }

有关表达式的更多信息,请参阅表达式

要使用以下内容定义$first的文档顺序:

注意

尽管$sort阶段将有序文档作为输入传递给$group$setWindowFields阶段,但不能保证这些阶段在自己的输出中保持排序顺序。

$setWindowFields 一起使用时,对于空$first null窗口 , 返回 。一个空窗口示例是{ documents: [ -1, -1 ] } 分区 第一个文档上的 文档 窗口。

群组中的文档可能缺少字段,或者可能具有缺失值的字段。

  • 如果前一个管道阶段没有文档,则$group阶段将不返回任何内容。

  • 如果$first累加器正在处理的字段缺失,则$first返回null

请参阅缺失数据示例。

创建 sales 集合:

db.sales.insertMany( [
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") },
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") },
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") },
{ "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-15T12:05:10Z") },
{ "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T14:12:12Z") }
] )

item字段对文档进行分组,以下操作使用$first累加器计算每件商品的首个销售日期:

db.sales.aggregate(
[
{ $sort: { item: 1, date: 1 } },
{
$group:
{
_id: "$item",
firstSale: { $first: "$date" }
}
}
]
)

操作返回以下结果:

[
{ _id: 'jkl', firstSale: ISODate("2014-02-03T09:00:00.000Z") },
{ _id: 'xyz', firstSale: ISODate("2014-02-03T09:05:00.000Z") },
{ _id: 'abc', firstSale: ISODate("2014-01-01T08:00:00.000Z") }
]

badData 集合中的部分文档缺少字段,其他文档则缺少字段值。

创建 badData 集合:

db.badData.insertMany( [
{ "_id": 1, "price": 6, "quantity": 6 },
{ "_id": 2, "item": "album", "price": 5 , "quantity": 5 },
{ "_id": 7, "item": "tape", "price": 6, "quantity": 6 },
{ "_id": 8, "price": 5, "quantity": 5 },
{ "_id": 9, "item": "album", "price": 3, "quantity": '' },
{ "_id": 10, "item": "tape", "price": 3, "quantity": 4 },
{ "_id": 12, "item": "cd", "price": 7 }
] )

查询 badData 集合,在 item 字段对输出进行分组:

db.badData.aggregate( [
{ $sort: { item: 1, price: 1 } },
{ $group:
{
_id: "$item",
inStock: { $first: "$quantity" }
}
}
] )

$sort阶段对文档进行排序并将它们传递到$group阶段。

[
{ _id: null, inStock: 5 },
{ _id: 'album', inStock: '' },
{ _id: 'cd', inStock: null },
{ _id: 'tape', inStock: 4 }
]

$first 从每个输出群组中选择第一个文档:

  • 包括 _id: null 组。

  • 当累加器字段(在本示例中为$quantity )缺失时, $first会返回null

版本 5.0 中的新增功能

创建cakeSales集合,其中包含加利福尼亚州 ( CA ) 和华盛顿州 ( WA ) 的蛋糕销售情况:

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 }
] )

此示例在$first $setWindowFields阶段使用type 为每个 输出第一个蛋糕销售订单state

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
firstOrderTypeForState: {
$first: "$type",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

在示例中:

  • partitionBy: "$state"state 对集合中的文档分区CAWA 都有分区。

  • sortBy: { orderDate: 1 }orderDate 以升序 (1) 对每个分区中的文档进行排序,因此最早的 orderDate 位于最前面。

  • outputfirstOrderTypeForState字段设置为文档窗口中的一阶type

    窗口包含介于unbounded下限和输出中current文档之间的文档。 这意味着$first返回分区开头和当前文档之间文档的第一个顺序type

在此输出中,CAWA 的第一个订单 type 值会显示在 firstOrderTypeForState 字段中:

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "firstOrderTypeForState" : "strawberry" }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "firstOrderTypeForState" : "strawberry" }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "firstOrderTypeForState" : "strawberry" }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "firstOrderTypeForState" : "strawberry" }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "firstOrderTypeForState" : "strawberry" }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "firstOrderTypeForState" : "strawberry" }
← $filter(聚合)
$firstN →