MongoDB
 SQL >> Database >  >> NoSQL >> MongoDB

Aggiungere valori e date con l'operatore $add di MongoDB

L'operatore di aggregazione $add di MongoDB somma valori. Questi valori possono essere tutti numeri oppure un mix di numeri e una data.

Accetta i valori come argomenti e ciascun argomento può essere qualsiasi espressione valida, purché tutti si risolvano in numeri, oppure in numeri più una data.

Dati di esempio

Supponiamo che una collezione chiamata data contenga questi documenti:

{ "_id" : 1, "a" : 100, "b" : 50, "c" : ISODate("2021-01-03T23:30:15.100Z") } { "_id" : 2, "a" : 20000, "b" : 15, "c" : ISODate("2019-12-08T04:00:20.112Z") } { "_id" : 3, "a" : 1700, "b" : 3, "c" : ISODate("2020-09-24T10:45:01.007Z") }

Sommare numeri

Possiamo sommare i campi a e b.

Esempio:

db.data.aggregate( [ { $project: { _id: 0, a: 1, b: 1, result: { $add: [ "$a", "$b" ] } } } ] )

Risultato:

{ "a" : 100, "b" : 50, "result" : 150 } { "a" : 20000, "b" : 15, "result" : 20015 } { "a" : 1700, "b" : 3, "result" : 1703 }

Sommare numeri a una data

Quando uno degli argomenti è una data, gli altri argomenti vengono interpretati come millisecondi da aggiungere.

Esempio:

db.data.aggregate( [ { $project: { _id: 0, a: 1, c: 1, result: { $add: [ "$a", "$c" ] } } } ] ).pretty()

Risultato:

{ "a" : 100, "c" : ISODate("2021-01-03T23:30:15.100Z"), "result" : ISODate("2021-01-03T23:30:15.200Z") } { "a" : 20000, "c" : ISODate("2019-12-08T04:00:20.112Z"), "result" : ISODate("2019-12-08T04:00:40.112Z") } { "a" : 1700, "c" : ISODate("2020-09-24T10:45:01.007Z"), "result" : ISODate("2020-09-24T10:45:02.707Z") }

I valori numerici del campo a sono stati ciascuno aggiunti, come millisecondi, al campo c.

Aggiungere più argomenti

Gli esempi precedenti sommavano due valori, ma puoi aggiungerne di più quando serve. Ecco tutti e tre i campi sommati:

db.data.aggregate( [ { $project: { _id: 0, a: 1, b: 1, c: 1, result: { $add: [ "$a", "$b", "$c" ] } } } ] ).pretty()

Risultato:

{ "a" : 100, "b" : 50, "c" : ISODate("2021-01-03T23:30:15.100Z"), "result" : ISODate("2021-01-03T23:30:15.250Z") } { "a" : 20000, "b" : 15, "c" : ISODate("2019-12-08T04:00:20.112Z"), "result" : ISODate("2019-12-08T04:00:40.127Z") } { "a" : 1700, "b" : 3, "c" : ISODate("2020-09-24T10:45:01.007Z"), "result" : ISODate("2020-09-24T10:45:02.710Z") }

Questa volta entrambi i numeri sono stati aggiunti alla data.

Solo una data consentita

Anche se puoi sommare molte espressioni, è ammessa una sola data. Passare più date produce un errore.

Esempio:

db.data.aggregate( [ { $project: { _id: 0, a: 1, b: 1, c: 1, result: { $add: [ "$c", ISODate("2020-09-24T10:45:01.007Z") ] } } } ] ).pretty()

Risultato:

uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "only one date allowed in an $add expression", "code" : 16612, "codeName" : "Location16612" } : 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

Passare nessun argomento

Passare nessun argomento a $add restituisce 0.

Esempio:

db.data.aggregate( [ { $project: { result: { $add: [ ] } } } ] )

Risultato:

{ "_id" : 1, "result" : 0 } { "_id" : 2, "result" : 0 } { "_id" : 3, "result" : 0 }

Passare valori null

Passare null restituisce null.

Esempio:

db.data.aggregate( [ { $project: { result: { $add: [ null ] } } } ] )

Risultato:

{ "_id" : 1, "result" : null } { "_id" : 2, "result" : null } { "_id" : 3, "result" : null }