In MongoDB, il $sin l'operatore della pipeline di aggregazione restituisce il seno di un valore misurato in radianti.
$sin accetta qualsiasi espressione valida che si risolve in un numero.
Il $sin operatore è stato introdotto in MongoDB 4.2.
Esempio
Supponiamo di avere una collezione chiamata test con il seguente documento:
{ "_id" : 1, "data" : 2 }
Possiamo usare il $sin operatore per restituire il seno dei data campo:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
sine: { $sin: "$data" }
}
}
]
) Risultato:
{ "sine" : 0.9092974268256817 } Converti in radianti
Come accennato, $sin restituisce il seno di un valore misurato in radianti. Se il valore è in gradi, puoi usare $degreesToRadians operatore per convertirlo in radianti.
Esempio:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
sine: { $degreesToRadians: { $sin: "$data" } }
}
}
]
) Risultato:
{ "sine" : 0.015870233978020357 } Valori decimali a 128 bit
Per impostazione predefinita, il $sin l'operatore restituisce valori come double , ma può anche restituire valori come decimali a 128 bit, purché l'espressione si risolva in un valore decimale a 128 bit.
Supponiamo di aggiungere il seguente documento alla nostra raccolta:
{ "_id" : 2, "data" : NumberDecimal("2.1301023541559787031443874490659") }
Eseguiamo il $sin operatore contro quel documento:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
sine: { $sin: "$data" }
}
}
]
) Risultato:
{ "sine" : NumberDecimal("0.8476235356531703096519423201190329") } L'output è decimale a 128 bit.
Valori Nulli
I valori Null restituiscono null quando si utilizza il $sin operatore.
Supponiamo di aggiungere il seguente documento alla nostra raccolta:
{ "_id" : 3, "data" : null }
Eseguiamo il $sin operatore contro quel documento:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
sine: { $sin: "$data" }
}
}
]
) Risultato:
{ "sine" : null }
Possiamo vedere che il risultato è null .
Valori NaN
Se l'argomento si risolve in NaN , $sin restituisce NaN .
Esempio:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
sine: { $sin: 1 * "$data" }
}
}
]
) Risultato:
{ "sine" : NaN } Campi inesistenti
Se il $sin operatore viene applicato a un campo che non esiste, null viene restituito.
Esempio:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
sine: { $sin: "$name" }
}
}
]
) Risultato:
{ "sine" : null } Infinito
Fornire Infinity o -Infinity restituirà un errore fuori intervallo.
Supponiamo di aggiungere il seguente documento alla raccolta:
{ "_id" : 4, "data" : Infinity }
Eseguiamo $sin ancora:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
sine: { $sin: "$data" }
}
}
]
) Risultato:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $sin to inf, value must in (-inf,inf)",
"code" : 50989,
"codeName" : "Location50989"
} : 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:618:17
example@sqldat.com/mongo/shell/assert.js:708:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1