May 20, 2021 WeChat Mini Program Development Document
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Evaluate the Boolean expression and return one of the two specified values.
The use of cond is as follows:
cond({ if: <布尔表达式>, then: <真值>, else: <假值> })
Or:
cond([ <布尔表达式>, <真值>, <假值> ])
In both forms, three parameters (if, then, else) are required.
If the Boolean expression is true, $cond will return the true value of the value, otherwise it will return the value of
Suppose the record of the collection items is as follows:
{ "_id": "0", "name": "item-a", "amount": 100 }
{ "_id": "1", "name": "item-b", "amount": 200 }
{ "_id": "2", "name": "item-c", "amount": 300 }
We can use cond to generate a new field discount based on the amount field:
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
name: 1,
discount: $.cond({
if: $.gte(['$amount', 200]),
then: 0.7,
else: 0.9
})
})
.end()
The output is as follows:
{ "_id": "0", "name": "item-a", "discount": 0.9 }
{ "_id": "1", "name": "item-b", "discount": 0.7 }
{ "_id": "2", "name": "item-c", "discount": 0.7 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Evaluates a given expression and returns an alternative value if the expression results in null, undefined, or does not exist;
s slt;expressions>, slt;alternative values s/he
IfNull is used in the following form:
ifNull([ <表达式>, <替代值> ])
Suppose the record of the collection items is as follows:
{ "_id": "0", "name": "A", "description": "这是商品A" }
{ "_id": "1", "name": "B", "description": null }
{ "_id": "2", "name": "C" }
We can use ifNull to add an alternative value to a document that does not have a desc field, or a document where the desc field is null.
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
_id: 0,
name: 1,
description: $.ifNull(['$description', '商品描述空缺'])
})
.end()
The output is as follows:
{ "name": "A", "description": "这是商品A" }
{ "name": "B", "description": "商品描述空缺" }
{ "name": "C", "description": "商品描述空缺" }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. The return value is calculated based on the given switch-case-default,
The use of switch is as follows:
switch({
branches: [
case: <表达式>, then: <表达式>,
case: <表达式>, then: <表达式>,
...
],
default: <表达式>
})
Suppose the record of the collection items is as follows:
{ "_id": "0", "name": "item-a", "amount": 100 }
{ "_id": "1", "name": "item-b", "amount": 200 }
{ "_id": "2", "name": "item-c", "amount": 300 }
We can use switch to generate a new field discount based on the amount field:
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
name: 1,
discount: $.switch({
branches: [
{ case: $.gt(['$amount', 250]), then: 0.8 },
{ case: $.gt(['$amount', 150]), then: 0.9 }
],
default: 1
})
})
.end()
The output is as follows:
{ "_id": "0", "name": "item-a", "discount": 1 }
{ "_id": "1", "name": "item-b", "discount": 0.9 }
{ "_id": "2", "name": "item-c", "discount": 0.8 }