运算符 $dayOfWeek
从日期值中提取星期几,其中 1 表示星期日,7 表示星期六。 它可用于根据星期几对文档进行分组或筛选。
Syntax
{ $dayOfWeek: <dateExpression> }
参数
参数 | Description |
---|---|
<dateExpression> |
要从中提取星期几的日期表达式。 |
例子
让我们了解 stores
数据集中的示例 json 的用法。
{
"_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"
}
示例 1:提取一周中的一天
查询使用 $dayOfWeek
运算符从 lastUpdated
时间戳中提取星期几。 根据 ISO-8601 排序,返回的值范围为 1(星期日)到 7(星期六)。
db.stores.aggregate([
{
$match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
},
{
$project: {
_id: 0,
dayOfWeek: { $dayOfWeek: "$lastUpdated" }
}
}
])
查询返回日期所在的星期 lastUpdated
几,从 1(星期日)到 7(星期六)的数字。
{
"dayOfWeek": "4"
}
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。