db.command.aggregate.objectToArray
#
1. 操作符描述功能:将一个对象转换为数组。方法把对象的每个键值对都变成输出数组的一个元素,元素形如 { k: <key>, v: <value> }
。
声明:db.command.aggregate.objectToArray(object)
#
2. 操作符参数字段 | 类型 | 必填 | 说明 |
---|---|---|---|
- | Object | 是 | 键值对对象 |
#
3. 示例代码假设集合 items
有如下记录:
{ "_id": 1, "attributes": { "color": "red", "price": 150 } }{ "_id": 2, "attributes": { "color": "blue", "price": 50 } }{ "_id": 3, "attributes": { "color": "yellow", "price": 10 } }
// 云函数环境下示例代码const tcb = require('@cloudbase/node-sdk')const app = tcb.init({ env: 'xxx'})
const db = app.database()const $ = db.command.aggregateconst _ = db.command
exports.main = async (event, context) => { const res = await db .collection('items') .aggregate() .project({ array: $.objectToArray('$attributes') }) .end() console.log(res.data)}
返回结果如下:
{ "_id": 1, "array": [{ "k": "color", "v": "red" }, { "k": "price", "v": 150 }] }{ "_id": 2, "array": [{ "k": "color", "v": "blue" }, { "k": "price", "v": 50 }] }{ "_id": 3, "array": [{ "k": "color", "v": "yellow" }, { "k": "price", "v": 10 }] }