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

SDK database Command, aggregate operator, count operator


May 20, 2021 WeChat Mini Program Development Document


Table of contents

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


AggregateCommand.abs(value: Expression<number>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the absolute value of a number.

Parameters

value: Expression<number>

number

Returns a value

Object

API description

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.

The sample code

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 }

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

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.

Parameters

value: Expression[]

(lt;expressions1>, slt;expressions2>, .)

Returns a value

Object

API description

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.

The sample code

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 numbers are the same

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 }

Increase the date value

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") }

AggregateCommand.ceil(value: Expression<number>): Object

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.

Parameters

value: Expression<number>

number

Returns a value

Object

API description

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.

The sample code

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 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Incoming dividers and divides, asking for business.

Parameters

value: Expression[]

The divided expression, the part, the number, the number, the

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.divide([<被除数表达式>, <除数表达式>])

An expression can be an expression that resolves arbitrarily to a number.

The sample code

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 }

AggregateCommand.exp(value: Expression<number>): Object

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).

Parameters

value: Expression<number>

exponent

Returns a value

Object

API description

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.

The sample code

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 }

AggregateCommand.floor(value: Expression<number>): Object

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.

Parameters

value: Expression<number>

number

Returns a value

Object

API description

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.

The sample code

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 }

AggregateCommand.ln(value: Expression<number>): Object

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.

Parameters

value: Expression<number>

number

Returns a value

Object

API description

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 sample code

db.command.aggregate.ln

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 ( .


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

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.

Parameters

value: Expression[]

[<number>, <base>]

Returns a value

Object

API description

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.

The sample code

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 }

AggregateCommand.log10(value: Expression<number>): Object

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.

Parameters

value: Expression<number>

number

Returns a value

Object

API description

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 sample code

db.command.aggregate.log10

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.


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

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.

Parameters

value: Expression[]

[<dividend>, <divisor>]

Returns a value

Object

API description

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.

The sample code

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 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Take the result of multiplying the incoming numeric parameters.

Parameters

value: Expression[]

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

Returns a value

Object

API description

The syntax is as follows:

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

Arguments can be expressions that resolve arbitrarily to numbers.

The sample code

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 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Find the exponential power of a given base.

Parameters

value: Expression[]

[<base>, <exponent>]

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.pow([<base>, <exponent>])

Arguments can be expressions that resolve arbitrarily to numbers.

The sample code

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 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Find the square root.

Parameters

value: Expression[]

[<number>]

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.sqrt([<number>])

Arguments can be expressions that resolve arbitrarily to non-negative numbers.

The sample code

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 }

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

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.

Parameters

value: Expression[]

[<expression1>, <expression2>]

Returns a value

Object

API description

The syntax is as follows:

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

Arguments can be expressions that resolve arbitrarily to numbers or dates.

The sample code

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 }

AggregateCommand.trunc(value: Expression<number>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Truncated the number to shape.

Parameters

value: Expression<number>

number

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.trunc(<number>)

Arguments can be expressions that resolve arbitrarily to numbers.

The sample code

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 }