db.command.aggregate.concatArrays
1. Operator Description
Function: Concatenates multiple arrays into a single array.
Declaration: db.command.aggregate.arrayToObject([ array1, array2, ... ])
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
- | <Array>Expression | Required | The expression can be any expression that resolves to an array. |
3. Sample Code
Suppose the collection items
contains the following records:
{ "_id": 1, "fruits": [ "apple" ], "vegetables": [ "carrot" ] }
{ "_id": 2, "fruits": [ "orange", "lemon" ], "vegetables": [ "cabbage" ] }
{ "_id": 3, "fruits": [ "strawberry" ], "vegetables": [ "spinach" ] }
// Sample code in the Cloud Function environment
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('items')
.aggregate()
.project({
sales: $.concatArrays(['$fruits', '$vegetables'])
})
.end()
console.log(res.data)
}
The returned result is as follows:
{ "_id": 1, "first": 80, "last": 90 }
{ "_id": 2, "first": 78, "last": 78 }
{ "_id": 3, "first": 95, "last": 92 }