Skip to main content

db.command.aggregate.dayOfWeek

1. Operator Description

Description: Returns the day of the week for a date field, which is an integer ranging from 1 (Sunday) to 7 (Saturday).

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

Notes: Sunday is the first day of the week.

2. Operator Parameters

FieldTypeRequiredDescription
-ExpressionRequiredDate field

3. Sample Code

Suppose the collection dates contains the following documents:

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

We use dayOfWeek() to project the date field and obtain the corresponding day of the week:

// 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

exports.main = async (event, context) => {
const res = await db
.collection('dates')
.aggregate()
.project({
_id: 0,
dayOfWeek: $.dayOfWeek('$date')
})
.end()
console.log(res.data)
}

The output is as follows:

{
"dayOfWeek": 3
}