Skip to main content

db.command.aggregate.and

1. Operator Description

Description: Given multiple expressions, and returns true only if all expressions return true; otherwise, it returns false.

Declaration: db.command.aggregate.and([expression1, expression2, ...])

Notes:

If the expression returns false, null, 0, or undefined, it resolves to false; otherwise, all other return values are considered true.

2. Operator Parameters

FieldTypeRequiredDescription
-<Array>ExpressionRequiredaggregate expression

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 greater than or equal to 30 and max is less than or equal to 80.

// 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('price')
.aggregate()
.project({
fullfilled: $.and([$.gte(['$min', 30]), $.lte(['$max', 80])])
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "_id": 1, "fullfilled": false }
{ "_id": 2, "fullfilled": true }
{ "_id": 3, "fullfilled": true }