$dateFromString演算子は、日付/時刻文字列を日付オブジェクトに変換するために使用されます。 この操作は、日付オブジェクトとして操作または照会する必要がある日付の文字列表現を処理する場合に便利です。
構文
{
$dateFromString: {
dateString: < string > ,
format: < string > ,
timezone: < string > ,
onError: < expression > ,
onNull: < expression >
}
}
パラメーター
| パラメーター | Description |
|---|---|
dateString |
日付オブジェクトに変換する日付/時刻文字列。 |
format |
(省略可能) dateStringの日付形式の指定。 |
timezone |
(省略可能)日付の書式設定に使用するタイムゾーン。 |
onError |
(省略可能) dateStringの解析中にエラーが発生した場合に返す値。 |
onNull |
(省略可能) dateString が null または不足している場合に返す値。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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 との機能の互換性について詳しくは、こちらをご覧ください。