May 20, 2021 WeChat Mini Program Development Document
1. Parameters
2. Returns a value
3. API description
4. The sample code
5. Parameters
6. Returns a value
7. API description
8. The sample code
9. Parameters
10. Returns a value
11. API description
12. The sample code
13. Parameters
14. Returns a value
15. API description
16. The sample code
17. Parameters
18. Returns a value
19. API description
20. The sample code
21. Parameters
22. Returns a value
23. API description
24. The sample code
25. Parameters
26. Returns a value
27. API description
28. The sample code
29. db.command.aggregate.ln
30. Parameters
31. Returns a value
32. API description
33. The sample code
34. Parameters
35. Returns a value
36. API description
37. The sample code
38. db.command.aggregate.log10
39. Parameters
40. Returns a value
41. API description
42. The sample code
43. Parameters
44. Returns a value
45. API description
46. The sample code
47. Parameters
48. Returns a value
49. API description
50. The sample code
51. Parameters
52. Returns a value
53. API description
54. The sample code
55. Parameters
56. Returns a value
57. API description
58. The sample code
59. Parameters
60. Returns a value
61. API description
62. The sample code
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Returns the absolute value of a number.
number
The syntax is as follows:
db.command.aggregate.abs(<number>)
The value passed in by abs can be any expression that eventually resolves to a number, in addition to the numeric constant.
If the expression resolves to null or points to a field that does not exist, the result of abs is null. If the value resolves to NaN, the result is NaN.
Suppose the collection ratings have the following records:
{ _id: 1, start: 5, end: 8 }
{ _id: 2, start: 4, end: 4 }
{ _id: 3, start: 9, end: 7 }
{ _id: 4, start: 6, end: 7 }
··· You can find the absolute difference size between start and end for each record by:
const $ = db.command.aggregate
db.collection('ratings').aggregate()
.project({
delta: $.abs($.subtract(['$start', '$end']))
})
.end()
The results are as follows:
{ "_id" : 1, "delta" : 3 }
{ "_id" : 2, "delta" : 0 }
{ "_id" : 3, "delta" : 2 }
{ "_id" : 4, "delta" : 1 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. A dd numbers or add numbers to the date. If one of the values in the array is a date, the other values are treated as milliseconds added to that date.
(lt;expressions1>, slt;expressions2>, .)
The syntax is as follows:
db.command.aggregate.add([<表达式1>, <表达式2>, ...])
An expression can be a field that is shaped like $ , or it can be a normal string. As long as it can be parsed into strings.
Suppose the collection staff has the following records:
{ _id: 1, department: "x", sales: 5, engineer: 10, lastUpdate: ISODate("2019-05-01T00:00:00Z") }
{ _id: 2, department: "y", sales: 10, engineer: 20, lastUpdate: ISODate("2019-05-01T02:00:00Z") }
{ _id: 3, department: "z", sales: 20, engineer: 5, lastUpdate: ISODate("2019-05-02T03:00:00Z") }
The total number of individual records can be as follows:
const $ = db.command.aggregate
db.collection('staff').aggregate()
.project({
department: 1,
total: $.add(['$sales', '$engineer'])
})
.end()
The results are as follows:
{ _id: 1, department: "x", total: 15 }
{ _id: 2, department: "y", total: 30 }
{ _id: 3, department: "z", total: 25 }
Here's how to get the lastUpdate for each record plus an hour later:
const $ = db.command.aggregate
db.collection('staff').aggregate()
.project({
department: 1,
lastUpdate: $.add(['$lastUpdate', 60*60*1000])
})
.end()
The results are as follows:
{ _id: 1, department: "x", lastUpdate: ISODate("2019-05-01T01:00:00Z") }
{ _id: 2, department: "y", lastUpdate: ISODate("2019-05-01T03:00:00Z") }
{ _id: 3, department: "z", lastUpdate: ISODate("2019-05-02T04:00:00Z") }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Round up to return a minimum integer that is greater than or equal to a given number.
number
The syntax is as follows:
db.command.aggregate.ceil(<number>)
The expression that can be any one that resolves to a number. If the expression resolves to null or points to a field that does not exist, null is returned, and NaN is returned if it is resolved to NaN.
Suppose the collection sales have the following records:
{ _id: 1, sales: 5.2 }
{ _id: 2, sales: 1.32 }
{ _id: 3, sales: -3.2 }
You can take the up rounding values of each number as follows:
const $ = db.command.aggregate
db.collection('sales').aggregate()
.project({
sales: $.ceil('$sales')
})
.end()
The results are as follows:
{ _id: 1, sales: 6 }
{ _id: 2, sales: 2 }
{ _id: 3, sales: -3 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Incoming dividers and divides, asking for business.
The divided expression, the part, the number, the number, the
The syntax is as follows:
db.command.aggregate.divide([<被除数表达式>, <除数表达式>])
An expression can be an expression that resolves arbitrarily to a number.
Suppose the collection railroads has the following records:
{ _id: 1, meters: 5300 }
{ _id: 2, meters: 64000 }
{ _id: 3, meters: 130 }
You can take the values after each number is converted to kilometers as follows:
const $ = db.command.aggregate
db.collection('railroads').aggregate()
.project({
km: $.divide(['$meters', 1000])
})
.end()
The results are as follows:
{ _id: 1, km: 5.3 }
{ _id: 2, km: 64 }
{ _id: 3, km: 0.13 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Take the nth square of e (the bottom of the natural pair, the Eurass).
exponent
The syntax is as follows:
db.command.aggregate.exp(<exponent>)
The expression can be any expression that resolves to a number. If the expression resolves to null or points to a field that does not exist, null is returned, and NaN is returned if it is resolved to NaN.
Suppose the collection math has the following records:
{ _id: 1, exp: 0 }
{ _id: 2, exp: 1 }
{ _id: 3, exp: 2 }
const $ = db.command.aggregate
db.collection('math').aggregate()
.project({
result: $.exp('$exp')
})
.end()
The results are as follows:
{ _id: 1, result: 1 }
{ _id: 2, result: 2.71828182845905 }
{ _id: 3, result: 7.38905609893065 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Round down to return a minimum integer that is greater than or equal to a given number.
number
The syntax is as follows:
db.command.aggregate.floor(<number>)
The expression that can be any one that resolves to a number. If the expression resolves to null or points to a field that does not exist, null is returned, and NaN is returned if it is resolved to NaN.
Suppose the collection sales have the following records:
{ _id: 1, sales: 5.2 }
{ _id: 2, sales: 1.32 }
{ _id: 3, sales: -3.2 }
You can take down the rounding values of each number as follows:
const $ = db.command.aggregate
db.collection('sales').aggregate()
.project({
sales: $.floor('$sales')
})
.end()
The results are as follows:
{ _id: 1, sales: 5 }
{ _id: 2, sales: 1 }
{ _id: 3, sales: -6 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Calculates a given number in a natural pair of values.
number
The syntax is as follows:
db.command.aggregate.ln(<number>)
An expression that can be resolved to a non-negative number at will.
ln is equivalent to log ( .
The aggregate operator. Calculates a given number in a natural pair of values.
The syntax is as follows:
db.command.aggregate.ln(<number>)
An expression that can be resolved to a non-negative number at will.
ln is equivalent to log ( .
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Calculates the log value of a given number at the bottom of a given log log.
[<number>, <base>]
The syntax is as follows:
db.command.aggregate.log([<number>, <base>])
An expression that can be resolved to a non-negative number at will. Can be any expression that resolves to a number greater than 1.
If either argument resolves to null or points to any field that does not exist, log returns null. If either parameter resolves to NaN, log returns NaN.
Suppose the collection curve has the following records:
{ _id: 1, x: 1 }
{ _id: 2, x: 2 }
{ _id: 3, x: 3 }
Calculate the value of log2(x):
const $ = db.command.aggregate
db.collection('staff').aggregate()
.project({
log: $.log(['$x', 2])
})
.end()
The results are as follows:
{ _id: 1, log: 0 }
{ _id: 2, log: 1 }
{ _id: 3, log: 1.58496250072 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Calculates the log value of a given number at the bottom of the log at the bottom of the log.
number
The syntax is as follows:
db.command.aggregate.log(<number>)
An expression that can be resolved to a non-negative number at will.
The second argument of the log10 equivalent to the log method is fixed at 10.
The aggregate operator. Calculates the log value of a given number at the bottom of the log at the bottom of the log.
The syntax is as follows:
db.command.aggregate.log(<number>)
An expression that can be resolved to a non-negative number at will.
The second argument of the log10 equivalent to the log method is fixed at 10.
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. The molding operation, taking the value after the digital molding.
[<dividend>, <divisor>]
The syntax is as follows:
db.command.aggregate.mod([<dividend>, <divisor>])
The first number is divided, and the second number is divided. Arguments can be expressions that resolve arbitrarily to numbers.
Suppose the collection shopping has the following records:
{ _id: 1, bags: 3, items: 5 }
{ _id: 2, bags: 2, items: 8 }
{ _id: 3, bags: 5, items: 16 }
Each record divides items by the remaining part of the bags (items % bags):
const $ = db.command.aggregate
db.collection('shopping').aggregate()
.project({
overflow: $.mod(['$items', '$bags'])
})
.end()
The results are as follows:
{ _id: 1, log: 2 }
{ _id: 2, log: 0 }
{ _id: 3, log: 1 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Take the result of multiplying the incoming numeric parameters.
[<expression1>, <expression2>, ...]
The syntax is as follows:
db.command.aggregate.multiply([<expression1>, <expression2>, ...])
Arguments can be expressions that resolve arbitrarily to numbers.
Suppose the collection fruits have the following records:
{ "_id": 1, "name": "apple", "price": 10, "quantity": 100 }
{ "_id": 2, "name": "orange", "price": 15, "quantity": 50 }
{ "_id": 3, "name": "lemon", "price": 5, "quantity": 20 }
Find the total value of each fruit:
const $ = db.command.aggregate
db.collection('fruits').aggregate()
.project({
name: 1,
total: $.multiply(['$price', '$quantity']),
})
.end()
The results are as follows:
{ "_id": 1, "name": "apple", "total": 1000 }
{ "_id": 2, "name": "orange", "total": 750 }
{ "_id": 3, "name": "lemo", "total": 100 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Find the exponential power of a given base.
[<base>, <exponent>]
The syntax is as follows:
db.command.aggregate.pow([<base>, <exponent>])
Arguments can be expressions that resolve arbitrarily to numbers.
Suppose the collection stats has the following records:
{ "_id": 1, "x": 2, "y": 3 }
{ "_id": 2, "x": 5, "y": 7 }
{ "_id": 3, "x": 10, "y": 20 }
Find the sum of the squares of x and y:
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
sumOfSquares: $.add([$.pow(['$x', 2]), $.pow(['$y', 2])]),
})
.end()
The results are as follows:
{ "_id": 1, "sumOfSquares": 13 }
{ "_id": 2, "sumOfSquares": 74 }
{ "_id": 3, "sumOfSquares": 500 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Find the square root.
[<number>]
The syntax is as follows:
db.command.aggregate.sqrt([<number>])
Arguments can be expressions that resolve arbitrarily to non-negative numbers.
Suppose the right triangle set triangle has the following record:
{ "_id": 1, "x": 2, "y": 3 }
{ "_id": 2, "x": 5, "y": 7 }
{ "_id": 3, "x": 10, "y": 20 }
Suppose x and y are two right-angled edges, respectively, and the sloped edges are long:
const $ = db.command.aggregate
db.collection('triangle').aggregate()
.project({
len: $.sqrt([$.add([$.pow(['$x', 2]), $.pow(['$y', 2])])]),
})
.end()
The results are as follows:
{ "_id": 1, "len": 3.605551275463989 }
{ "_id": 2, "len": 8.602325267042627 }
{ "_id": 3, "len": 22.360679774997898 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Subtract two numbers and return the difference, or subtract the two dates and then return the difference of milliseconds, or subtract one date from the date on which a number returns the result.
[<expression1>, <expression2>]
The syntax is as follows:
db.command.aggregate.subtract([<expression1>, <expression2>])
Arguments can be expressions that resolve arbitrarily to numbers or dates.
Suppose the collection scores have the following records:
{ "_id": 1, "max": 10, "min": 1 }
{ "_id": 2, "max": 7, "min": 5 }
{ "_id": 3, "max": 6, "min": 6 }
Find the difference between max and min for each record. :
const $ = db.command.aggregate
db.collection('scores').aggregate()
.project({
diff: $.subtract(['$max', '$min'])
})
.end()
The results are as follows:
{ "_id": 1, "diff": 9 }
{ "_id": 2, "diff": 2 }
{ "_id": 3, "diff": 0 }
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregate operator. Truncated the number to shape.
number
The syntax is as follows:
db.command.aggregate.trunc(<number>)
Arguments can be expressions that resolve arbitrarily to numbers.
Suppose the collection scores have the following records:
{ "_id": 1, "value": 1.21 }
{ "_id": 2, "value": 3.83 }
{ "_id": 3, "value": -4.94 }
const $ = db.command.aggregate
db.collection('scores').aggregate()
.project({
int: $.trunc('$value')
})
.end()
The results are as follows:
{ "_id": 1, "value": 1 }
{ "_id": 2, "value": 3 }
{ "_id": 3, "value": -4 }