運算子 $dateFromString 可用來將日期/時間字串轉換成 date 物件。 處理需要作或查詢為日期物件的日期字串表示法時,這項作業很有用。
語法
{
$dateFromString: {
dateString: < string > ,
format: < string > ,
timezone: < string > ,
onError: < expression > ,
onNull: < expression >
}
}
參數
| 參數 | Description |
|---|---|
dateString |
要轉換成 date 物件的日期/時間字串。 |
format |
(選擇性)的 dateString日期格式規格。 |
timezone |
(選擇性)用來格式化日期的時區。 |
onError |
(選擇性)剖析 dateString時發生錯誤時要傳回的值。 |
onNull |
(選擇性)如果 dateString 為 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 日期
此查詢會從 $concat 個別年、月和日欄位建構完整的 ISO 日期字串,並使用 $dateFromString將其轉換為 startDate 和 endDate。 當日期元件分別儲存在檔中時,會很有用。
db.stores.aggregate([
{
$match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
},
{
$unwind: "$promotionEvents"
},
{
$project: {
eventName: "$promotionEvents.eventName",
startDate: {
$dateFromString: {
dateString: {
$concat: [
{ $toString: "$promotionEvents.promotionalDates.startDate.Year" },
"-",
{
$cond: {
if: { $lt: ["$promotionEvents.promotionalDates.startDate.Month", 10] },
then: { $concat: ["0", { $toString: "$promotionEvents.promotionalDates.startDate.Month" }] },
else: { $toString: "$promotionEvents.promotionalDates.startDate.Month" }
}
},
"-",
{
$cond: {
if: { $lt: ["$promotionEvents.promotionalDates.startDate.Day", 10] },
then: { $concat: ["0", { $toString: "$promotionEvents.promotionalDates.startDate.Day" }] },
else: { $toString: "$promotionEvents.promotionalDates.startDate.Day" }
}
}
]
}
}
},
endDate: {
$dateFromString: {
dateString: {
$concat: [
{ $toString: "$promotionEvents.promotionalDates.endDate.Year" },
"-",
{
$cond: {
if: { $lt: ["$promotionEvents.promotionalDates.endDate.Month", 10] },
then: { $concat: ["0", { $toString: "$promotionEvents.promotionalDates.endDate.Month" }] },
else: { $toString: "$promotionEvents.promotionalDates.endDate.Month" }
}
},
"-",
{
$cond: {
if: { $lt: ["$promotionEvents.promotionalDates.endDate.Day", 10] },
then: { $concat: ["0", { $toString: "$promotionEvents.promotionalDates.endDate.Day" }] },
else: { $toString: "$promotionEvents.promotionalDates.endDate.Day" }
}
}
]
}
}
}
}
}
])
此查詢會傳回下列結果。
[
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"eventName": "Massive Markdown Mania",
"startDate": "2024-09-21T00:00:00.000Z",
"endDate": "2024-09-29T00:00:00.000Z"
}
]
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。