$dateToString演算子は、日付オブジェクトを指定した形式の文字列に変換するために使用されます。 集計パイプラインでは、レポート、クエリ、または表示の目的で日付フィールドの書式を設定するために一般的に使用されます。 この演算子は汎用性が高く、カスタムの日付形式を定義できます。
構文
{
$dateToString: {
format: "<format_string>",
date: <date_expression>,
timezone: "<timezone>",
onNull: "<replacement_value>"
}
}
パラメーター
| パラメーター | Description |
|---|---|
format |
出力日の形式を指定する文字列。 |
date |
書式設定する日付式。 |
timezone |
(省略可能)タイムゾーンを指定する文字列。 指定しない場合、既定値は UTC です。 |
onNull |
(省略可能) date フィールドが null または不足している場合に返す値。 |
書式指定子
| Symbol | Meaning |
|---|---|
%Y |
年 (4 桁) |
%m |
月 (2 桁) |
%d |
月の日 (2 桁) |
%H |
時間 (24 時間、2 桁) |
%M |
分 (2 桁) |
%S |
2 番目 (2 桁) |
%L |
ミリ秒 (3 桁) |
例示
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 に似た文字列に日付フィールドを書式設定する
このクエリでは、 $dateToString 演算子を使用して、 lastUpdated タイムスタンプを YYYY-MM-DD 文字列に書式設定します。 ログ、レポート、または UI に適した読み取り可能な形式で日付を表示するのに役立ちます。
db.stores.aggregate([
{
$match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
},
{
$project: {
_id: 0,
formattedDate: {
$dateToString: {
format: "%Y-%m-%d",
date: "$lastUpdated"
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"formattedDate": "2024-12-04"
}
]
例 2: NULL 値の処理
このクエリは、存在しないフィールドlastUpdated_newタイムスタンプを、YYYY-MM-DDを使用して$dateToString文字列として書式設定します。 日付が見つからないか null であることを考慮して、onNull オプションを使用してフォールバック文字列 "No date available" に置き換えます。
db.stores.aggregate([
{
$match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
},
{
$project: {
_id: 0,
formattedDateOrDefault: {
$dateToString: {
format: "%Y-%m-%d",
date: "$lastUpdated_new", // field doesn't exist
onNull: "No date available"
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"formattedDateOrDefault": "No date available"
}
]