该 $isoWeekYear 运算符以 ISO 8601 格式返回年份号。 ISO 周编号年可以不同于日历年,日期在年初或年底。 ISO 周年是包含有问题的周四的年份。
Syntax
{
$isoWeekYear: <dateExpression>
}
参数
| 参数 | Description |
|---|---|
dateExpression |
解析为 Date、Timestamp 或 ObjectId 的表达式。 如果表达式解析为 null 或缺失, $isoWeekYear 则返回 null。 |
例子
请考虑商店集合中的这个示例文档。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
示例 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"
}
},
endDate: {
$dateFromParts: {
year: "$promotionEvents.promotionalDates.endDate.Year",
month: "$promotionEvents.promotionalDates.endDate.Month",
day: "$promotionEvents.promotionalDates.endDate.Day"
}
}
}
},
{
$project: {
eventName: 1,
startDate: 1,
endDate: 1,
startCalendarYear: { $year: "$startDate" },
startISOWeekYear: { $isoWeekYear: "$startDate" },
endCalendarYear: { $year: "$endDate" },
endISOWeekYear: { $isoWeekYear: "$endDate" },
yearDifference: {
$ne: [{ $year: "$startDate" }, { $isoWeekYear: "$startDate" }]
}
}
},
{ $match: {"eventName": "Discount Delight Days" } }
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Discount Delight Days",
"startDate": "2023-12-26T00:00:00.000Z",
"endDate": "2024-01-05T00:00:00.000Z",
"startCalendarYear": 2023,
"startISOWeekYear": Long("2023"),
"endCalendarYear": 2024,
"endISOWeekYear": Long("2024"),
"yearDifference": true
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。