db.command.aggregate.arrayElemAt
#
1. 操作符描述功能:返回在指定数组下标的元素。
声明:db.command.aggregate.arrayElemAt([array, index])
#
2. 操作符参数字段 | 类型 | 必填 | 说明 |
---|---|---|---|
- | <Array>Expression | 是 | 第一个元素array 可以是任意解析为数组的表达式,第二个元素index 可以是任意解析为整形的表达式。 |
注意事项:
如果是正数,
arrayElemAt
返回在index
位置的元素,如果是负数,arrayElemAt
返回从数组尾部算起的index
位置的元素。
#
3. 示例代码假设集合 exams
有如下记录:
{ "_id": 1, "scores": [80, 60, 65, 90] }{ "_id": 2, "scores": [78] }{ "_id": 3, "scores": [95, 88, 92] }
求各个第一次考试的分数和和最后一次的分数:
// 云函数环境下示例代码const tcb = require('@cloudbase/node-sdk')const app = tcb.init({ env: 'xxx'})
const db = app.database()const $ = db.command.aggregate
exports.main = async (event, context) => { const res = await db .collection('exams') .aggregate() .project({ first: $.arraElemAt(['$scores', 0]), last: $.arraElemAt(['$scores', -1]) }) .end() console.log(res.data)}
返回结果如下:
{ "_id": 1, "first": 80, "last": 90 }{ "_id": 2, "first": 78, "last": 78 }{ "_id": 3, "first": 95, "last": 92 }