db.command.aggregate.isoWeek
1. Operator Description
Function: Returns the ISO 8601 week number (the week of the year) for a date field, which is an integer ranging from 1 to 53.
Declaration: db.command.aggregate.isoWeek(<date field>)
For example, January 7, 2016 is the first Thursday of that year, so the week from Monday, 2016.01.04 to Sunday, 2016.01.10 is week 1. Similarly, the week number for January 1, 2016 is 53.
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
- | Expression | Required | aggregate expression |
3. Sample Code
Suppose the collection dates
contains the following documents:
{
"_id": 1,
"date": ISODate("2019-05-14T09:38:51.686Z")
}
We use isoWeek()
to project the date
field and obtain the corresponding ISO 8601 week number (the week of the year):
// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})
const db = app.database()
const $ = db.command.aggregate
const _ = db.command
exports.main = async (event, context) => {
const res = await db
.collection('dates')
.aggregate()
.project({
_id: 0,
isoWeek: $.isoWeek('$date')
})
.end()
console.log(res.data)
}
The output is as follows:
{
"isoWeek": 20
}