文章目录
$first
聚合运算符返回分组中第一个文档指定表达式的结果,只有分组文档是有序的情况下才有意义。
$first
可用于下列阶段:
$bucket
$bucketAuto
$group
setWindowFields
语法
js
{ $first: <expression> }
$
不需要任何参数。
使用
定义文档顺序
- 在
$setWindowFields
阶段,需要使用sortBy
字段来定义文档顺序 - 对于其他管道阶段,可以在其前面增加一个
$sort
阶段
数组操作
如果表达式解析为数组,则:
- 对于一组文档,
$first
返回第一个文档的整个数组,不会遍历数组元素,类似于$group
和$setWindowFields
阶段。 - 对于单个文档,
$first
则返回数组的第一个元素,这类似于$addFields
阶段
缺失值的处理
分组内的文档可能会有字段或字段值缺失,对于这种情况:
- 如果前一管道阶段没有文档输入,则
$group
阶段什么也不返回 - 如果字段
$first
处理的字段不存在,则返回null - 对于
$setWindowFields
,空窗口返回null,比如分区的第一个文档的文档窗口为{ documents:[ -1, -1] }
举例
使用下面的脚本创建sales
集合:
js
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
运算符返回每个条目中的首次销售日期:
js
db.sales.aggregate(
[
{ $sort: { item: 1, date: 1 } },
{
$group:
{
_id: "$item",
firstSale: { $first: "$date" }
}
}
]
)
操作返回下面的结果:
json
[
{ _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
集合,其中有些字段缺失,有些值缺失:
js
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
字段分组并使用$fist
将第一个$quantity
的值放入inStock
字段输出:
js
db.badData.aggregate( [
{ $sort: { item: 1, price: 1 } },
{ $group:
{
_id: "$item",
inStock: { $first: "$quantity" }
}
}
] )
结果如下:
js
[
{ _id: null, inStock: 5 },
{ _id: 'album', inStock: '' },
{ _id: 'cd', inStock: null },
{ _id: 'tape', inStock: 4 }
]
在$setWindowFields阶段中使用
使用下面的脚本创建cakeSales
集合:
js
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 }
] )
此示例在$setWindowFields
阶段使用$first
输出每个州的第一个蛋糕销售订单类型:
js
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
firstOrderTypeForState: {
$first: "$type",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
在本例中:
partitionBy: "$state"
对集合中的文档按照州state
进行分区,分别为CA
和WA
sortBy: { orderDate: 1 }
对每个分区内的文档按照orderDate
从小到大进行排序,$orderDate
最早的在最前面output
将文档窗口内的第一个文档的订单类型type
赋予一个新字段firstOrderTypeForState
。
执行结果如下:
json
{ "_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" }