On this page本页内容
$first
¶Returns the value that results from applying an expression to the first document in a group of documents that share the same group by key. 返回将表达式应用于按键共享同一组文档中的第一个文档所产生的值。Only meaningful when documents are in a defined order.仅当文档按定义的顺序排列时才有意义。
$first
is only available in the $group
stage.$first
仅在$group
阶段可用。
Disambiguation消除歧义
The following page describes the accumulator 下页介绍了累加器$first
, available only within the $group
stage. $first
,它仅在$group
阶段中可用。For the array operator 对于数组运算符$first
, refer to the $first (array operator)
instead.$first
,请参考$first
(数组运算符)。
$first
has the following syntax:语法如下所示:
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
When using 在$first
in a $group
stage, the $group
stage should follow a $sort
stage to have the input documents in a defined order.$group
阶段中使用$first
时,$group
阶段应该在$sort
阶段之后,以使输入文档按定义的顺序排列。
Consider a 考虑一个sales
collection with the following documents:sales
集合带有以下文档:
Grouping the documents by the 按照item
field, the following operation uses the $first
accumulator to compute the first sales date for each item:item
字段对文档进行分组,以下操作使用$first
累加器计算每个物料的第一个销售日期:
The operation returns the following results:操作返回以下结果: