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

SDK database Command-aggregate operator-date 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

57. Parameters

58. Returns a value

59. API description

60. The sample code

61. Parameters

62. Returns a value

63. API description

64. The sample code

65. Parameters

66. Returns a value

67. API description

68. The sample code


AggregateCommand.dateFromParts(value: any): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. For information about a given date, build and return a date object.

Parameters

value: any

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.dateFromParts({
    year: <year>,
    month: <month>,
    day: <day>,
    hour: <hour>,
    minute: <minute>,
    second: <second>,
    millisecond: <ms>,
    timezone: <tzExpression>
})

You can also use the ISO 8601 standard:

db.command.aggregate.dateFromParts({
    isoWeekYear: <year>,
    isoWeek: <week>,
    isoDayOfWeek: <day>,
    hour: <hour>,
    minute: <minute>,
    second: <second>,
    millisecond: <ms>,
    timezone: <tzExpression>
})

The sample code

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    date: $.dateFromParts({
        year: 2017,
        month: 2,
        day: 8,
        hour: 12,
        timezone: 'America/New_York'
    }),
  })
  .end()

The output is as follows:

{
    "date": ISODate("2017-02-08T17:00:00.000Z")
}

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.dayOfMonth(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of days (which day of the month) for the date field, which is a number between 1 and 31.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.dayOfMonth(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use dayOfMonth() to project the date field to get the corresponding date:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    dayOfMonth: $.dayOfMonth('$date')
  })
  .end()

The output is as follows:

{
    "dayOfMonth": 14
}

AggregateCommand.dayOfWeek(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of days (the day of the week) corresponding to the date field, which is an integer between 1 (Sunday) and 7 (Saturday).

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

Note: Sunday is the 1st day of the week

The syntax is as follows:

db.command.aggregate.dayOfWeek(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use dayOfWeek() to project the date field to get the corresponding number of days (the first day of the week):

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    dayOfWeek: $.dayOfWeek('$date')
  })
  .end()

The output is as follows:

{
    "dayOfWeek": 3
}

AggregateCommand.dayOfYear(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of days (the first day of the year) for the date field, which is an integer between 1 and 366.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.dayOfYear(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use dayOfYear() to project the date field to get the corresponding number of days (the first day of the year):

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    dayOfYear: $.dayOfYear('$date')
  })
  .end()

The output is as follows:

{
    "dayOfYear": 134
}

AggregateCommand.hour(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. The number of hours corresponding to the return date field is an integer between 0 and 23.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.hour(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use hour() to project the date field for the corresponding number of hours:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    hour: $.hour('$date')
  })
  .end()

The output is as follows:

{
    "hour": 9
}

AggregateCommand.isoDayOfWeek(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of days of the ISO 8601 standard (the day of the week) corresponding to the date field, which is an integer between 1 (Monday) and 7 (Sunday).

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.month(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use month() to project the date field to get the corresponding number of days of the ISO 8601 standard (day of the week):

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    isoDayOfWeek: $.isoDayOfWeek('$date')
  })
  .end()

The output is as follows:

{
    "isoDayOfWeek": 2
}

AggregateCommand.isoWeek(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of weeks of the ISO 8601 standard (the first few weeks of the year) corresponding to the date field, which is an integer between 1 and 53.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

According to ISO 8601, Monday through Sunday is considered a week, and the week in which the first Thursday of the year is held is considered the first week of the year.

For example, january 7, 2016 is the first Thursday of the year, then Monday 2016.01.04 to Sunday 2016.01.10 is week 1. Similarly, the number of weeks on January 1, 2016 was 53.

The syntax is as follows:

db.command.aggregate.isoWeek(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use isoWeek() to project the date field to get the corresponding number of weeks of the ISO 8601 standard (the first few weeks of the year):

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    isoWeek: $.isoWeek('$date')
  })
  .end()

The output is as follows:

{
    "isoWeek": 20
}

AggregateCommand.isoWeekYear(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of days (the first day of the year) corresponding to the ISO 8601 standard for the date field.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The Year here begins on Monday in the first week and ends on Sunday in the last week.

The syntax is as follows:

db.command.aggregate.isoWeekYear(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use isoWeekYear() to project the date field to get the corresponding number of days of the ISO 8601 standard (the first day of the year):

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    isoWeekYear: $.isoWeekYear('$date')
  })
  .end()

The output is as follows:

{
    "isoWeekYear": 2019
}

AggregateCommand.millisecond(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of milliseconds corresponding to the date field, which is an integer between 0 and 999.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.millisecond(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use millisecond() to project the date field to get the corresponding number of milliseconds:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    millisecond: $.millisecond('$date'),
  })
  .end()

The output is as follows:

{
    "millisecond": 686
}

AggregateCommand.minute(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. The number of minutes corresponding to the return date field is an integer between 0 and 59.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.minute(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use minute() to project the date field for the corresponding number of minutes:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    minute: $.minute('$date')
  })
  .end()

The output is as follows:

{
    "minute": 38
}

AggregateCommand.month(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the month corresponding to the date field, which is an integer between 1 and 12.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.month(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use month() to project the date field to get the corresponding month:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    month: $.month('$date')
  })
  .end()

The output is as follows:

{
    "month": 5
}

AggregateCommand.second(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of seconds corresponding to the date field, which is an integer between 0 and 59, and in exceptional cases (leap seconds) may be equal to 60.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.second(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use second() to project the date field for the corresponding number of seconds:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    second: $.second('$date')
  })
  .end()

The output is as follows:

{
    "second": 51
}

AggregateCommand.week(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the number of weeks (the first few weeks of the year) for the date field, which is an integer between 0 and 53.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The week begins on Sunday, and the first Sunday of the year begins with week 1, which is before week 0.

The syntax is as follows:

db.command.aggregate.week(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use week() to project the date field to get the corresponding number of weeks (the first few weeks of the year):

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    week: $.week('$date')
  })
  .end()

The output is as follows:

{
    "week": 19
}

AggregateCommand.year(value: Expression<string>): Object

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregate operator. Returns the year for which the date field corresponds.

Parameters

value: Expression<string>

The date field

Returns a value

Object

API description

The syntax is as follows:

db.command.aggregate.year(<日期字段>)

The sample code

Suppose the collection dates have the following documents:

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

We use year() to project the date field to get the corresponding year:

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    year: $.year('$date')
  })
  .end()

The output is as follows:

{
    "year": 2019
}

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 }