db.command.aggregate.or
1. Operator Description
Function: Given multiple expressions, or
returns true
if any expression evaluates to true
; otherwise, it returns false
.
Declaration: db.command.aggregate.or([expression1, expression2, ...])
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
- | <Array>Expression | Required | array of aggregate expressions |
3. Sample Code
Suppose the collection price
contains the following records:
{ "_id": 1, "min": 10, "max": 100 }
{ "_id": 2, "min": 60, "max": 80 }
{ "_id": 3, "min": 30, "max": 50 }
Find records where min
is less than 40 and max
is greater than 60.
// 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
const _ = db.command
exports.main = async (event, context) => {
const res = await db
.collection('price')
.aggregate()
.project({
fullfilled: $.or([$.lt(['$min', 30]), $.gt(['$max', 60])])
})
.end()
console.log(res.data)
}
The returned result is as follows:
{ "_id": 1, "fullfilled": true }
{ "_id": 2, "fullfilled": false }
{ "_id": 3, "fullfilled": true }