Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
SI APPLICA A: MongoDB vCore
L'operatore $convert converte un'espressione in un valore del tipo specificato. L'operatore $convert esegue anche un'azione specificata se la conversione dell'espressione di input nel tipo specificato non riesce.
Sintassi
La sintassi per l'operatore $convert
è:
{ "$convert": {"input": <expression>, "to": <type>, "format": <binData format>, "onError": <value to return on error>, "onNull": <value to return on null> }
Parametri
Parametro | Descrizione |
---|---|
input |
Valore di input da convertire nel tipo specificato |
to |
Tipo in cui convertire il valore di input |
format |
(Facoltativo) Formato binData dell'input o dell'output durante la conversione di un valore in o da binData |
onError |
(Facoltativo) Valore da restituire quando la conversione dell'input nel tipo specificato ha esito negativo |
onNull |
(Facoltativo) Valore da restituire quando il valore di input da convertire nel tipo specificato è null o mancante |
Esempi
Si consideri questo documento di esempio dalla raccolta di negozi nel database StoreData.
{
"_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
}
]
}
]
}
Esempio 1: Convertire un valore Int in una stringa
db.stores.aggregate([
{
"$match": {
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
}
},
{
"$project": {
"fulltimeStaff": "$staff.totalStaff.fullTime",
"fulltimeStaffAsString": {
"$convert": {
"input": "$staff.totalStaff.fullTime",
"to": "string"
}
}
}
}])
Questa query restituisce il risultato seguente:
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"fulltimeStaff": 3,
"fulltimeStaffAsString": "3"
}
Esempio 2: Convertire un valore Int in un valore booleano
db.stores.aggregate([
{
"$match": {
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
}
},
{
"$project": {
"fulltimeStaff": "$staff.totalStaff.fullTime",
"fulltimeStaffAsBool": {
"$convert": {
"input": "$staff.totalStaff.fullTime",
"to": "bool"
}
}
}
}])
Questa query restituisce il risultato seguente:
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"fulltimeStaff": 3,
"fulltimeStaffAsBool": true
}
Esempio 3: Convertire un valore Int in un valore Decimal
db.stores.aggregate([
{
"$match": {
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
}
},
{
"$project": {
"fulltimeStaff": "$staff.totalStaff.fullTime",
"fulltimeStaffAsDecimal": {
"$convert": {
"input": "$staff.totalStaff.fullTime",
"to": "decimal"
}
}
}
}])
Questa query restituisce il risultato seguente:
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"fulltimeStaff": 3,
"fulltimeStaffAsDecimal": "Decimal128('3')"
}
Esempio 4: Convertire un valore Int in un valore Long
db.stores.aggregate([
{
"$match": {
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
}
},
{
"$project": {
"fulltimeStaff": "$staff.totalStaff.fullTime",
"fulltimeStaffAsLong": {
"$convert": {
"input": "$staff.totalStaff.fullTime",
"to": "long"
}
}
}
}])
Questa query restituisce il risultato seguente:
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"fulltimeStaff": 3,
"fulltimeStaffAsLong": "Long('3')"
}