Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Der $dateFromString Operator wird verwendet, um eine Datums-/Uhrzeitzeichenfolge in ein Datumsobjekt zu konvertieren. Dieser Vorgang kann beim Umgang mit Zeichenfolgendarstellungen von Datumsangaben hilfreich sein, die als Datumsobjekte bearbeitet oder abgefragt werden müssen.
Syntax
{
$dateFromString: {
dateString: < string > ,
format: < string > ,
timezone: < string > ,
onError: < expression > ,
onNull: < expression >
}
}
Die Parameter
| Parameter | Description |
|---|---|
dateString |
Die Datums-/Uhrzeitzeichenfolge, die in ein Datumsobjekt konvertiert werden soll. |
format |
(Optional) Die Datumsformatspezifikation der dateString. |
timezone |
(Optional) Die Zeitzone, die zum Formatieren des Datums verwendet werden soll. |
onError |
(Optional) Der zurückzugebende Wert, wenn beim Analysieren des Fehlers ein dateStringFehler auftritt. |
onNull |
(Optional) Der Wert, der zurückgegeben werden soll, wenn dies dateString der Wert ist null oder fehlt. |
Examples
Betrachten Sie dieses Beispieldokument aus der Stores-Sammlung.
{
"_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
}
]
}
]
}
Beispiel 1: Konvertieren von Werbeereignisdaten in ISO-Datumsangaben
Diese Abfrage erstellt vollständige ISO-Datumszeichenfolgen aus einzelnen Feldern für Jahr, Monat und Tag und $concat konvertiert sie mithilfe von "startDate" und "endDate" in "startDate" und "endDate" $dateFromString. Es ist nützlich, wenn Datumskomponenten separat in Dokumenten gespeichert werden.
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" }
}
}
]
}
}
}
}
}
])
Diese Abfrage gibt das folgende Ergebnis zurück.
[
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"eventName": "Massive Markdown Mania",
"startDate": "2024-09-21T00:00:00.000Z",
"endDate": "2024-09-29T00:00:00.000Z"
}
]
Verwandte Inhalte
- Überprüfen Sie die Optionen für die Migration von MongoDB zu Azure DocumentDB.
- Weitere Informationen zur Featurekompatibilität mit MongoDB.