Supported platforms: Mini Program 2.7.4, Cloud Function 0.8.1, Web
An aggregation operator. Determines whether the given expression is an array and returns a Boolean value.
Parameters
value: Expression
Expression
Return Values
Object
API Description
The syntax is as follows:
db.command.aggregate.isArray(<expression>)
The parameter can be any expression.
Sample Code
Assume the collection stats has the following records:
{
"_id": 1,
"base": 10,
"sales": [ 1, 6, 2, 2, 5 ]
}
{
"_id": 2,
"base": 1,
"sales": 100
}
To calculate the total sales: if sales is a number, calculate sales * base; if sales is an array, calculate the product of the sum of array elements and base.
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
sum: $.cond({
if: $.isArray('$sales'),
then: $.multiply([$.sum(['$sales']), '$base']),
else: $.multiply(['$sales', '$base']),
})
})
.end()
The returned result is as follows:
{ "_id": 1, "index": 160 }
{ "_id": 2, "index": 100 }