Skip to main content

db.command.aggregate.isoWeekYear

1. Operator Description

Function: Returns the ISO 8601 day of the year for a date field.

Declaration: db.command.aggregate.isoWeekYear(<date field>)

Notes:

Here, a "year" starts with the Monday of the first week and ends with the Sunday of the last week.

2. Operator Parameters

FieldTypeRequiredDescription
-ExpressionRequiredaggregate expression

3. Sample Code

Suppose the collection dates contains the following documents:

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

We use isoWeekYear() to project the date field and obtain the corresponding ISO 8601 day number (the day 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,
isoWeekYear: $.isoWeekYear('$date')
})
.end()
console.log(res.data)
}

The output is as follows:

{
"isoWeekYear": 2019
}