In MongoDB, il $round l'operatore della pipeline di aggregazione arrotonda un numero a un intero intero o a una cifra decimale specificata.
Hai la possibilità di specificare per quante cifre decimali arrotondare il numero. Per fare ciò, passa un secondo argomento. Il primo argomento è il numero da arrotondare e il secondo argomento (facoltativo) è il numero di cifre decimali a cui arrotondarlo.
Esempio
Supponiamo di avere una collezione chiamata test con i seguenti documenti:
{ "_id" : 1, "data" : 8.99 }
{ "_id" : 2, "data" : 8.45 }
{ "_id" : 3, "data" : 8.451 }
{ "_id" : 4, "data" : -8.99 }
{ "_id" : 5, "data" : -8.45 }
{ "_id" : 6, "data" : -8.451 }
{ "_id" : 7, "data" : 8 }
{ "_id" : 8, "data" : 0 }
Possiamo usare il $round per arrotondare i valori nei data campo:
db.test.aggregate(
[
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data" ] }
}
}
]
) Risultato:
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 } Specifica un punto decimale
Abbiamo la possibilità di utilizzare un secondo argomento per specificare di quante cifre decimali arrotondare il numero.
Esempio:
db.test.aggregate(
[
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data", 1 ] }
}
}
]
) Risultato:
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 } Punti decimali negativi
Il secondo argomento può essere qualsiasi espressione valida che si risolve in un numero intero compreso tra -20 e 100, esclusivo. Pertanto, è possibile specificare una cifra decimale negativa.
Quando si esegue questa operazione, il numero viene arrotondato a sinistra della cifra decimale. Se il valore assoluto dell'intero negativo è maggiore del numero, il risultato è 0 .
Supponiamo di aggiungere i seguenti documenti alla nostra raccolta:
{ "_id" : 9, "data" : 8111.32 }
{ "_id" : 10, "data" : 8514.321 }
{ "_id" : 11, "data" : 8999.454 }
Ecco un esempio di utilizzo di vari decimali negativi quando si applica $round a quei documenti:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 9, 10, 11 ] } } },
{
$project:
{
_id: 0,
data: 1,
a: { $round: [ "$data", -1 ] },
b: { $round: [ "$data", -2 ] },
c: { $round: [ "$data", -3 ] },
d: { $round: [ "$data", -4 ] },
e: { $round: [ "$data", -5 ] }
}
}
]
).pretty() Risultato:
{
"data" : 8111.32,
"a" : 8110,
"b" : 8100,
"c" : 8000,
"d" : 10000,
"e" : 0
}
{
"data" : 8514.321,
"a" : 8510,
"b" : 8500,
"c" : 9000,
"d" : 10000,
"e" : 0
}
{
"data" : 8999.454,
"a" : 9000,
"b" : 9000,
"c" : 9000,
"d" : 10000,
"e" : 0
} Punto decimale di zero
Quando fornisci una cifra decimale di 0 , il $round l'operatore arrotonda utilizzando la prima cifra a destra del decimale e restituisce il valore intero arrotondato.
Esempio:
db.test.aggregate(
[
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data", 0 ] }
}
}
]
) Risultato:
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
{ "data" : 8111.32, "rounded" : 8111 }
{ "data" : 8514.321, "rounded" : 8514 }
{ "data" : 8999.454, "rounded" : 8999 } Tipi di numeri
Il numero da arrotondare può essere qualsiasi espressione valida che si risolve in un numero intero, doppio, decimale o lungo. Il valore restituito corrisponde al tipo di dati del valore di input.
Quindi se aggiungiamo i seguenti documenti alla nostra collezione:
{ "_id" : 12, "data" : NumberDecimal("128.4585") }
{ "_id" : 13, "data" : NumberDecimal("128.12345678912") }
Possiamo applicare $round ai data campo:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 12, 13 ] } } },
{
$project:
{
_id: 0,
data: 1,
a: { $round: [ "$data", -1 ] },
b: { $round: [ "$data", 0 ] },
c: { $round: [ "$data", 3 ] },
d: { $round: [ "$data", 4 ] },
e: { $round: [ "$data", 5 ] }
}
}
]
).pretty() Risultato:
{
"data" : NumberDecimal("128.4585"),
"a" : NumberDecimal("1.3E+2"),
"b" : NumberDecimal("128"),
"c" : NumberDecimal("128.458"),
"d" : NumberDecimal("128.4585"),
"e" : NumberDecimal("128.45850")
}
{
"data" : NumberDecimal("128.12345678912"),
"a" : NumberDecimal("1.3E+2"),
"b" : NumberDecimal("128"),
"c" : NumberDecimal("128.123"),
"d" : NumberDecimal("128.1235"),
"e" : NumberDecimal("128.12346")
} Arrotondamento ai decimali nulli
Se il secondo argomento è null , il risultato è null .
Esempio:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3 ] } } },
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data", null ] }
}
}
]
) Risultato:
{ "data" : 8.99, "rounded" : null }
{ "data" : 8.45, "rounded" : null }
{ "data" : 8.451, "rounded" : null } Arrotondamento di un valore nullo
Se il valore da arrotondare è null , il risultato è null .
Supponiamo di aggiungere il seguente documento alla raccolta:
{ "_id" : 14, "data" : null }
E usiamo $round per arrotondare il valore nullo:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 14 ] } } },
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data", null ] }
}
}
]
) Risultato:
{ "data" : null, "rounded" : null } Infinito arrotondato
Se il numero da arrotondare è Infinity , il risultato è Infinity . Allo stesso modo, se è -Infinity , il risultato è -Infinity .
Aggiungiamo due documenti con tali valori:
{ "_id" : 15, "data" : Infinity }
{ "_id" : 16, "data" : -Infinity } E giriamoli intorno:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 15, 16 ] } } },
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data", 2 ] }
}
}
]
) Risultato:
{ "data" : Infinity, "rounded" : Infinity }
{ "data" : -Infinity, "rounded" : -Infinity } Arrotondamento NaN
Arrotondamento NaN risulta in NaN .
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data" * 2 ] }
}
}
]
) Risultato:
{ "data" : 8.99, "rounded" : NaN }
{ "data" : 8.45, "rounded" : NaN } Tipi non numerici
Se provi ad arrotondare un valore che è il tipo di dati sbagliato (cioè non è un numero intero, doppio, decimale o lungo), viene restituito un errore.
Supponiamo di aggiungere il seguente documento alla nostra raccolta:
{ "_id" : 17, "data" : "Thirty five" }
E ora proviamo ad arrotondare i data campo:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 17 ] } } },
{
$project:
{
_id: 0,
data: 1,
rounded: { $round: [ "$data" ] }
}
}
]
) Risultato:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$round only supports numeric types, not string",
"code" : 51081,
"codeName" : "Location51081"
} : aggregate failed :
example@sqldat.com/mongo/shell/utils.js:25:13
example@sqldat.com/mongo/shell/assert.js:18:14
example@sqldat.com/mongo/shell/assert.js:639:17
example@sqldat.com/mongo/shell/assert.js:729:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1058:12
@(shell):1:1