Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The $dayOfWeek
operator extracts the day of the week from a date value, where 1 represents Sunday and 7 represents Saturday. It's useful for grouping or filtering documents based on the day of the week.
Syntax
{ $dayOfWeek: <dateExpression> }
Parameters
Parameter | Description |
---|---|
<dateExpression> |
The date expression from which to extract the day of the week. |
Examples
Let's understand the usage with sample json from stores
dataset.
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"name": "Relecloud | Toy Collection - North Jaylan",
"location": {
"lat": 2.0797,
"lon": -94.4134
},
"staff": {
"employeeCount": {
"fullTime": 7,
"partTime": 4
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "Educational Toys",
"totalSales": 3299
}
],
"revenue": 3299
},
"promotionEvents": [
{
"eventName": "Massive Markdown Mania",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Remote Control Toys",
"discountPercentage": 6
},
{
"categoryName": "Building Sets",
"discountPercentage": 21
}
]
}
],
"company": "Relecloud",
"city": "North Jaylan",
"lastUpdated": {
"$timestamp": {
"t": 1733313006,
"i": 1
}
},
"storeOpeningDate": "2024-09-05T11:50:06.549Z"
}
Example 1: Extract day of the week
The query uses the $dayOfWeek
operator to extract the day of the week from the lastUpdated
timestamp. The returned value ranges from 1 (Sunday) to 7 (Saturday), based on ISO-8601 ordering.
db.stores.aggregate([
{
$match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
},
{
$project: {
_id: 0,
dayOfWeek: { $dayOfWeek: "$lastUpdated" }
}
}
])
The query returns which day of the week the lastUpdated
date falls on, as a number from 1 (Sunday) through 7 (Saturday).
{
"dayOfWeek": "4"
}
Related content
- Review options for migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore).
- Read more about feature compatibility with MongoDB.