Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SDK database Command.aggregate operator Boolean operator


May 20, 2021 WeChat Mini Program Development Document


Table of contents


AggregateCommand.and(value: Expression[]): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Given multiple expressions, and returns true only when all expressions return true, otherwise false is returned.

Parameters

value: Expression[]

[<expression1>, <expression2>, ...]

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.and([<expression1>, <expression2>, ...])

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

The sample code

Suppose the collection price has the following records:

{ "_id": 1, "min": 10, "max": 100 }
{ "_id": 2, "min": 60, "max": 80 }
{ "_id": 3, "min": 30, "max": 50 }

a record with min greater than or equal to 30 and max greater than or equal to 80.

const $ = db.command.aggregate
db.collection('price').aggregate()
  .project({
    fullfilled: $.and([$.gte(['$min', 30]), $.lte(['$max', 80])])
  })
  .end()

The results are as follows:

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

AggregateCommand.not(value: Expression): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. G iven an expression, if the expression returns true, not returns false, otherwise true is returned. Note that expressions cannot be logical expressions (and, or, nor, not).

Parameters

value: Expression

The expression

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.not(<expression>)

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

The sample code

Suppose the collection price has the following records:

{ "_id": 1, "min": 10, "max": 100 }
{ "_id": 2, "min": 60, "max": 80 }
{ "_id": 3, "min": 30, "max": 50 }

Min is not greater than 40 records.

const $ = db.command.aggregate
db.collection('price').aggregate()
  .project({
    fullfilled: $.not($.gt(['$min', 40]))
  })
  .end()

The results are as follows:

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

AggregateCommand.or(value: Expression[]): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Given multiple expressions, if any one expression returns true, or returns true, otherwise false is returned.

Parameters

value: Expression[]

[<expression1>, <expression2>, ...]

Returns a value

Objectu

API description

The syntax is as follows:

db.command.aggregate.or([<expression1>, <expression2>, ...])

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

The sample code

Suppose the collection price has the following records:

{ "_id": 1, "min": 10, "max": 100 }
{ "_id": 2, "min": 60, "max": 80 }
{ "_id": 3, "min": 30, "max": 50 }

A record with min less than 40 and max greater than 60.

const $ = db.command.aggregate
db.collection('price').aggregate()
  .project({
    fullfilled: $.or([$.lt(['$min', 30]), $.gt(['$max', 60])])
  })
  .end()

The results are as follows:

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