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

SDK database Command-aggregate operator-string 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. Parameters

30. Returns a value

31. API description

32. The sample code

33. Parameters

34. Returns a value

35. API description

36. The sample code

37. Parameters

38. Returns a value

39. API description

40. The sample code

41. Parameters

42. Returns a value

43. API description

44. The sample code

45. Parameters

46. Returns a value

47. API description

48. The sample code

49. Parameters

50. Returns a value

51. API description

52. The sample code

53. Parameters

54. Returns a value

55. API description

56. The sample code


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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. The connection string, which returns the stitched string.

Parameters

value: Expression[]

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

Returns a value

Object

API description

The syntax for concat is as follows:

db.command.aggregate.concat([<表达式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 records for the collection students are as follows:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

With concat, you can stitch the lastName and firstName fields to get each student's full name:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    fullName: $.concat(['$firstName', ' ', '$lastName'])
  })
  .end()

The results returned are as follows:

{ "fullName": "Yuanxin Dong" }
{ "fullName": "Weijia Wang" }
{ "fullName": "Chengxi Li" }

AggregateCommand.dateFromString(value: any): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Convert a date/time string to a date object

Parameters

value: any

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.dateFromString({
    dateString: <dateStringExpression>,
    timezone: <tzExpression>
})

The sample code

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    date: $.dateFromString({
        dateString: "2019-05-14T09:38:51.686Z"
    })
  })
  .end()

The output is as follows:

{
    "date": ISODate("2019-05-14T09:38:51.686Z")
}

AggregateCommand.dateToString(value: any): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Format the date object as a string that meets the requirements based on the specified expression.

Parameters

value: any

Returns a value

Object

API description

DateToString is called as follows:

db.command.aggregate.dateToString({
  date: <日期表达式>,
  format: <格式化表达式>,
  timezone: <时区表达式>,
  onNull: <空值表达式>
})

Here's a detailed description of the four expressions:

Name Describe
The date expression Required. The specified field value should be the date on which it can be converted into a string.
Format the expression Optional. It can be any valid string that contains a "format descriptor."
Time zone expression Optional. I ndicates the time zone for the result of the operation. It resolves strings formatted as UTC Offset or Olson Timezone Identifier.
An empty value expression Optional. When <日期表达式> the value indicated by this expression is returned.

Here's a detailed description of the format descriptor:

The descriptor Describe The legal value
%d Date of month (2 digits, 0 fill) 01 - 31
%G Year in ISO 8601 format 0000 - 9999
%H Hours (2 digits, 0 fills, 24-hour) 00 - 23
%j One day of the year (3 digits, 0 fills) 001 - 366
%L milliseconds (3 digits, 0 fills) 000 - 999
%m Month (2 digits, 0 fill) 01 - 12
%M Minutes (2 digits, 0 fills) 00 - 59
%S Seconds (2 digits, 0 fills) 00 - 60
%w What day of the week 1 - 7
%u The day of the week in ISO 8601 format 1 - 7
%U One week of the year (2 digits, 0 fills) 00 - 53
%V IsO 8601 format for one week of the year 1 - 53
%Y Year (4 digits, 0 fill) 0000 - 9999
%z The time zone offset from the UTC +/-[hh][mm]
%Z Offset from UTC's time zone in minutes +/-mmm
%% The percent sign is the character %

The sample code

Suppose the collection students have the following records:

{ "date": "1999-12-11T16:00:00.000Z", "firstName": "Yuanxin", "lastName": "Dong" }
{ "date": "1998-11-10T16:00:00.000Z", "firstName": "Weijia", "lastName": "Wang" }
{ "date": "1997-10-09T16:00:00.000Z", "firstName": "Chengxi", "lastName": "Li" }

Format the date

Here is the value of the date field, formatted as a year-month-date string:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    formatDate: $.dateToString({
      date: '$date',
      format: '%Y-%m-%d'
    })
  })
  .end()

The results returned are as follows:

{ "formatDate": "1999-12-11" }
{ "formatDate": "1998-11-10" }
{ "formatDate": "1997-10-09" }

Time zone time

Here is an example of formatting the date field value as Shanghai time zone time:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    formatDate: $.dateToString({
      date: '$date',
      format: '%H:%M:%S',
      timezone: 'Asia/Shanghai'
    })
  })
  .end()

The results returned are as follows:

{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }

The default value for the missing condition

You can set the default value in the absence of the specified date expression when it returns an empty or non-existent value:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    formatDate: $.dateToString({
      date: '$empty',
      onNull: 'null'
    })
  })
  .end()

The results returned are as follows:

{ "formatDate": "null" }
{ "formatDate": "null" }
{ "formatDate": "null" }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. L ook for substrings in the target string and return the byte index of UTF-8 that first appears, starting at 0. If a substring does not exist, return -1.

Parameters

value: Expression[]

The target string expression, the target string expression, the substring expression, the start position expression, the end position expression, the start position expression, the starting position expression, the starting position expression, the

Returns a value

Object

API description

The syntax of indexOfBytes is as follows:

db.command.aggregate.indexOfBytes([<目标字符串表达式>, <子字符串表达式>, <开始位置表达式>, <结束位置表达式>])

Here is a detailed description of the four expressions:

The expression Describe
The target string expression Any expression that can be parsed as a string
Substring expression Any expression that can be parsed as a string
The start position expression Any expression that can be resolved to a non-negative integer
End position expression Any expression that can be resolved to a non-negative integer

The sample code

Suppose the records for the collection students are as follows:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Find the position of the character "a" in the field firstName with indexOfBytes:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    aStrIndex: $.indexOfBytes(['$firstName', 'a'])
  })
  .end()

The results returned are as follows:

{ "aStrIndex": 2 }
{ "aStrIndex": 5 }
{ "aStrIndex": -1 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. L ook for substrings in the target string and return the code point index of UTF-8, which first appears, starting at 0. If a substring does not exist, return -1.

Parameters

value: Expression[]

The target string expression, the target string expression, the substring expression, the start position expression, the end position expression, the start position expression, the starting position expression, the starting position expression, the

Returns a value

Object

API description

Code point is the code point, also known as the code location. This refers to the code points in the Unicode package, ranging from 0 (16th) to 10FFFF (16th).

IndexOfCP's syntax is as follows:

db.command.aggregate.indexOfCP([<目标字符串表达式>, <子字符串表达式>, <开始位置表达式>, <结束位置表达式>])

Here is a detailed description of the four expressions:

The expression Describe
The target string expression Any expression that can be parsed as a string
Substring expression Any expression that can be parsed as a string
The start position expression Any expression that can be resolved to a non-negative integer
End position expression Any expression that can be resolved to a non-negative integer

The sample code

Suppose the records for the collection students are as follows:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Find the position of the character "a" in the field firstName with indexOfCP:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    aStrIndex: $.indexOfCP(['$firstName', 'a'])
  })
  .end()

The results returned are as follows:

{ "aStrIndex": 2 }
{ "aStrIndex": 5 }
{ "aStrIndex": -1 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. S eparate the array by separator, and remove the separator, returning an array of substrings. If the string cannot be separated by a separator, the original string is returned as the unique element of the array.

Parameters

value: Expression[]

the string expression, the separator expression, the string

Returns a value

Object

API description

Split's syntax is as follows:

db.command.aggregate.split([<字符串表达式>, <分隔符表达式>])

String expressions and separator expressions can be expressions in any form, as long as they can be parsed as strings.

The sample code

Suppose the records for the collection students are as follows:

{ "birthday": "1999/12/12" }
{ "birthday": "1998/11/11" }
{ "birthday": "1997/10/10" }

The birthday field corresponding values in each record are separated by split, and each array consists of three elements representing the year, month, and day:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    birthday: $.split(['$birthday', '/'])
  })
  .end()

The results returned are as follows:

{ "birthday": [ "1999", "12", "12" ] }
{ "birthday": [ "1998", "11", "11" ] }
{ "birthday": [ "1997", "10", "10" ] }

AggregateCommand.strLenBytes(value: Expression): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Calculates and returns the number of bytes encoded by utf-8 in the specified string.

Parameters

value: Expression

The expression

Returns a value

Object

API description

The syntax of strLenBytes is as follows:

db.command.aggregate.strLenBytes(<表达式>)

An expression is a valid expression as long as it can be parsed as a string.

The sample code

Suppose the records for the collection students are as follows:

{ "name": "dongyuanxin", "nickname": "心谭" }

Use strLenBytes to calculate the byte length of the values corresponding to the name field and the nickname field:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    nameLength: $.strLenBytes('$name'),
    nicknameLength: $.strLenBytes('$nickname')
  })
  .end()

The results are as follows:

{ "nameLength": 11, "nicknameLength": 6 }

AggregateCommand.strLenCP(value: Expression): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Calculate and return the number of UTC-8 code points for the specified string.

Parameters

value: Expression

The expression

Returns a value

Object

API description

The syntax of strLenCP is as follows:

db.command.aggregate.strLenCP(<表达式>)

An expression is a valid expression as long as it can be parsed as a string.

The sample code

Suppose the records for the collection students are as follows:

{ "name": "dongyuanxin", "nickname": "心谭" }

Use strLenCP to calculate the number of UTC-8 code points for the values corresponding to the name and nickname fields:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    nameLength: $.strLenCP('$name'),
    nicknameLength: $.strLenCP('$nickname')
  })
  .end()

The results are as follows:

{ "nameLength": 11, "nicknameLength": 2 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Make a case-insensession-sensitive size comparison of the two strings and return the results of the comparison.

Parameters

value: Expression[]

(lt;expressions1>, slt;expressions2?gt;)

Returns a value

Object

API description

The syntax of strcasecmp is as follows:

db.command.aggregate.strcasecmp([<表达式1>, <表达式2>])

Expressions 1 and 2 are valid as long as they can be parsed into strings.

The results of the comparison returned are 1,0 and -1:

  • 1: String resolved by Expression 1 and resolved by Expression 2
  • 0: Strings resolved by expression 1 - Strings resolved by expression 2
  • -1: String resolved by Expression 1 and resolved by Expression 2

The sample code

Suppose the records for the collection students are as follows:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Compare the size of the firstName field value with the lastName field value with the strcasecmp:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    result: $.strcasecmp(['$firstName', '$lastName']),
  })
  .end()

The results are as follows:

{ "result": 1 }
{ "result": 1 }
{ "result": -1 }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. R eturns a substring of the specified length of the string starting at the specified location. It is an alias for db.command.aggregate.substrBytes and is more recommended.

Parameters

value: Expression[]

(lt;expressions1>, slt;expressions 2>, slt;expressions3>)

Returns a value

Object

API description

Substr's syntax is as follows:

db.command.aggregate.substr([<表达式1>, <表达式2>, <表达式3>])

Expression 1 is any valid expression that can be parsed as a string, and expression 2 and expression 3 are any valid expression that can be resolved to a number.

If expression 2 is negative, the result returned is "".

If Expression 3 is negative, the result is the starting position specified from Expression 2 and the substrings that follow.

The sample code

Suppose the records for the collection students are as follows:

{ "birthday": "1999/12/12", "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "birthday": "1998/11/11", "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "birthday": "1997/10/10", "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Substr allows you to extract year, month, and day information from birthday, as follows:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    year: $.substr(['$birthday', 0, 4]),
    month: $.substr(['$birthday', 5, 2]),
    day: $.substr(['$birthday', 8, -1])
  })
  .end()

The results returned are as follows:

{ "day": "12", "month": "12", "year": "1999" }
{ "day": "11", "month": "11", "year": "1998" }
{ "day": "10", "month": "10", "year": "1997" }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. R eturns a substring of the specified length of the string starting at the specified location. Substrings start with characters in the UTF-8 byte index specified in the string and are the number of bytes specified.

Parameters

value: Expression[]

(lt;expressions1>, slt;expressions 2>, slt;expressions3>)

Returns a value

Object

API description

The syntax of substrBytes is as follows:

db.command.aggregate.substrBytes([<表达式1>, <表达式2>, <表达式3>])

Expression 1 is any valid expression that can be parsed as a string, and expression 2 and expression 3 are any valid expression that can be resolved to a number.

If expression 2 is negative, the result returned is "".

If Expression 3 is negative, the result is the starting position specified from Expression 2 and the substrings that follow.

The sample code

Suppose the records for the collection students are as follows:

{ "birthday": "1999/12/12", "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "birthday": "1998/11/11", "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "birthday": "1997/10/10", "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

SubstrBytes allows you to extract year, month, and day information from birthday, as follows:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    year: $.substrBytes(['$birthday', 0, 4]),
    month: $.substrBytes(['$birthday', 5, 2]),
    day: $.substrBytes(['$birthday', 8, -1])
  })
  .end()

The results returned are as follows:

{ "day": "12", "month": "12", "year": "1999" }
{ "day": "11", "month": "11", "year": "1998" }
{ "day": "10", "month": "10", "year": "1997" }

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

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. R eturns a substring of the specified length of the string starting at the specified location. Substrings start with characters in the UTF-8 byte index specified in the string and are the number of bytes specified.

Parameters

value: Expression[]

(lt;expressions1>, slt;expressions 2>, slt;expressions3>)

Returns a value

Object

API description

The syntax of substrCP is as follows:

db.command.aggregate.substrCP([<表达式1>, <表达式2>, <表达式3>])

Expression 1 is any valid expression that can be parsed as a string, and expression 2 and expression 3 are any valid expression that can be resolved to a number.

If expression 2 is negative, the result returned is "".

If Expression 3 is negative, the result is the starting position specified from Expression 2 and the substrings that follow.

The sample code

Suppose the records for the collection students are as follows:

{ "name": "dongyuanxin", "nickname": "心谭" }

SubstrCP allows you to extract the first character of the nickname field value:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    firstCh: $.substrCP(['$nickname', 0, 1])
  })
  .end()

The results returned are as follows:

{ "firstCh": "心" }

AggregateCommand.toLower(value: any): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Convert the string into a small case and return it.

Parameters

value: any

Returns a value

Object

API description

The syntax for toLower is as follows:

db.command.aggregate.toLower(表达式)

An expression is a valid expression as long as it can be parsed as a string. For example: $ , specify the field.

The sample code

Suppose the records for the collection students are as follows:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Turn firstName's field values into small case with toLower:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    result: $.toLower('$firstName'),
  })
  .end()

The results returned are as follows:

{ "result": "yuanxin" }
{ "result": "weijia" }
{ "result": "chengxi" }

AggregateCommand.toUpper(value: any): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Convert the string to capital and return.

Parameters

value: any

Returns a value

Object

API description

The syntax for toUpper is as follows:

db.command.aggregate.toUpper(表达式)

An expression is a valid expression as long as it can be parsed as a string. For example: $ , specify the field.

The sample code

Suppose the records for the collection students are as follows:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Turn the field values of lastName into capitals with toUpper:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    result: $.toUpper('$lastName'),
  })
  .end()

The results returned are as follows:

{ "result": "DONG" }
{ "result": "WANG" }
{ "result": "LI" }