$dayOfWeek
The $dayOfWeek operator in Amazon DocumentDB extracts the day of the week from a given date field. It returns the day of the week as a number between 1 (Sunday) and 7 (Saturday), which is the same behavior as in MongoDB.
Parameters
-
date field: The date field to extract the day of the week from.
Example (MongoDB Shell)
This example demonstrates how to use the $dayOfWeek operator to extract the day of the week from the date field in the weather collection.
Create sample documents
db.weather.insertMany([ { "temperature": 97.5, "humidity": 0.60, "date": new Date("2023-04-01") }, { "temperature": 95.2, "humidity": 0.55, "date": new Date("2023-04-02") }, { "temperature": 92.8, "humidity": 0.65, "date": new Date("2023-04-03") } ]);
Query example
db.weather.aggregate([ { $project: { dayOfWeek: { $dayOfWeek: "$date" } } } ]).pretty();
Output
{ "_id" : ObjectId("64272c6663f4f8ce422c2d91"), "dayOfWeek" : 7 }
{ "_id" : ObjectId("64272c6663f4f8ce422c2d92"), "dayOfWeek" : 1 }
{ "_id" : ObjectId("64272c6663f4f8ce422c2d93"), "dayOfWeek" : 2 }
Code examples
To view a code example for using the $dayOfWeek command, choose the tab for the language that you want to use: