Sha*_*bob 1 mongodb aggregation-framework
我需要从数组中提取第一项并将其添加到它自己的对象中,所以我发现 $first 正是这样做的https://docs.mongodb.com/manual/reference/operator/aggregation/first-array-element /#example
但是,我收到错误Unrecognized expression '$first'并认为这是我的查询不起作用,所以我克隆了他们在他们的文档中的确切示例,但它给了我同样的错误。
db.runninglog.insertMany([
{ "_id" : 1, "team" : "Anteater", log: [ { run: 1, distance: 8 }, { run2: 2, distance: 7.5 }, { run: 3, distance: 9.2 } ] },
{ "_id" : 2, "team" : "Bears", log: [ { run: 1, distance: 18 }, { run2: 2, distance: 17 }, { run: 3, distance: 16 } ] },
{ "_id" : 3, "team" : "Cobras", log: [ { run: 1, distance: 2 } ] }
])
db.runninglog.aggregate([
{ $addFields: { firstrun: { $first: "$log" }, lastrun: { $last: "$log" } } }
])
Run Code Online (Sandbox Code Playgroud)
预期:文档的预期。
已收到:Unrecognized expression '$first'
编辑
我找到了另一种方法来做同样的事情, { $addFields: { firstrun: { $arrayElemAt: [ "$log", 0 ] } } }但我仍然对上述内容感到好奇
来自文档:4.4 版中的新功能
请注意,您尝试使用的是 $first 数组元素运算符,与$group 阶段中可用的$first 聚合累加器不同。
| 归档时间: |
|
| 查看次数: |
2097 次 |
| 最近记录: |