你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用对象: MongoDB vCore
运算符 $isoWeek
以 ISO 8601 格式返回年份的周数,范围为 1 到 53。 运算符接受解析为 Date、Timestamp 或 ObjectId 的日期表达式。 在 ISO 8601 中,周从周一开始,一年中的第一周是包含今年第一个星期四的周。
语法
运算符的 $isArray
语法如下所示:
{
$isoWeek: <dateExpression>
}
参数
DESCRIPTION | |
---|---|
dateExpression |
解析为 Date、Timestamp 或 ObjectId 的表达式。 如果表达式解析为 null 或缺失, $isoWeek 则返回 null。 |
示例:
让我们了解数据集中示例 JSON 的 stores
用法。
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"promotionEvents": [
{
"eventName": "Massive Markdown Mania",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 6,
"Day": 29
},
"endDate": {
"Year": 2023,
"Month": 7,
"Day": 9
}
}
}
]
}
示例 1:获取促销事件的 ISO 周编号
该示例演示如何提取促销事件开始日期的 ISO 周号。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{ $unwind: "$promotionEvents" },
{
$project: {
eventName: "$promotionEvents.eventName",
startDate: {
$dateFromParts: {
year: "$promotionEvents.promotionalDates.startDate.Year",
month: "$promotionEvents.promotionalDates.startDate.Month",
day: "$promotionEvents.promotionalDates.startDate.Day"
}
}
}
},
{
$project: {
eventName: 1,
startDate: 1,
isoWeekNumber: { $isoWeek: "$startDate" },
year: { $year: "$startDate" }
}
}
])
该查询返回每个促销事件开始日期的 ISO 周号。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Massive Markdown Mania",
"startDate": "2023-06-29T00:00:00.000Z",
"isoWeekNumber": 26,
"year": 2023
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Fantastic Deal Days",
"startDate": "2023-09-27T00:00:00.000Z",
"isoWeekNumber": 39,
"year": 2023
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Discount Delight Days",
"startDate": "2023-12-26T00:00:00.000Z",
"isoWeekNumber": 52,
"year": 2023
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Super Sale Spectacular",
"startDate": "2024-03-25T00:00:00.000Z",
"isoWeekNumber": 13,
"year": 2024
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Grand Deal Days",
"startDate": "2024-06-23T00:00:00.000Z",
"isoWeekNumber": 25,
"year": 2024
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Major Bargain Bash",
"startDate": "2024-09-21T00:00:00.000Z",
"isoWeekNumber": 38,
"year": 2024
}
]