In MongoDB, $and l'operatore della pipeline di aggregazione valuta una o più espressioni e restituisce true se tutti restituiscono true , o se viene invocato senza argomenti. Altrimenti restituisce false .
Sintassi
La sintassi è questa:
{ $and: [ <expression1>, <expression2>, ... ] } Esempio
Supponiamo di avere una raccolta chiamata data con il seguente documento:
{ "_id" : 1, "a" : 10, "b" : 2, "c" : 20 }
Ecco cosa succede quando utilizziamo $and per verificare due condizioni rispetto a quel documento:
db.data.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
a: 1,
b: 1,
result: { $and: [
{ $gt: [ "$a", 9 ] },
{ $lt: [ "$b", 3 ] }
] }
}
}
]
) Risultato:
{ "a" : 10, "b" : 2, "result" : true }
Possiamo vedere che $and restituito true .
Più di due argomenti
Come accennato, $and accetta una o più espressioni. L'esempio precedente utilizza due espressioni. Ecco un esempio che ne utilizza tre:
db.data.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
a: 1,
b: 1,
c: 1,
result: { $and: [
{ $gt: [ "$a", 9 ] },
{ $lt: [ "$b", 3 ] },
{ $gt: [ "$c", 30 ] }
] }
}
}
]
) Risultato:
{ "a" : 10, "b" : 2, "c" : 20, "result" : false }
In questo caso il risultato è false , perché la terza espressione restituisce false . Se un'espressione restituisce false , il risultato è false ($and richiede che tutte le espressioni siano true prima che possa restituire true ).
Un argomento
Dato che $and accetta una o più espressioni, è possibile fornire un solo argomento.
Esempio:
db.data.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
a: 1,
result: { $and: [ { $gt: [ "$a", 9 ] } ] } }
}
]
) Risultato:
{ "a" : 10, "result" : true } Valori zero, nulli e non definiti
Il $and l'operatore valuta 0 , null e undefined come false .
Supponiamo di avere i seguenti documenti:
{ "_id" : 2, "a" : 0, "b" : 2 }
{ "_id" : 3, "a" : 10, "b" : 0 }
{ "_id" : 4, "a" : 0, "b" : 0 }
{ "_id" : 5, "a" : null, "b" : 2 }
{ "_id" : 6, "a" : 10, "b" : null }
{ "_id" : 7, "a" : null, "b" : null }
{ "_id" : 8, "a" : undefined, "b" : 2 }
{ "_id" : 9, "a" : 10, "b" : undefined }
{ "_id" : 10, "a" : undefined, "b" : undefined }
Ecco cosa succede quando applichiamo $and :
db.data.aggregate(
[
{ $match: { _id: { $in: [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ] } } },
{ $project: {
_id: 0,
a: 1,
b: 1,
result: { $and: [ "$a", "$b" ] } }
}
]
) Risultato:
{ "a" : 0, "b" : 2, "result" : false }
{ "a" : 10, "b" : 0, "result" : false }
{ "a" : 0, "b" : 0, "result" : false }
{ "a" : null, "b" : 2, "result" : false }
{ "a" : 10, "b" : null, "result" : false }
{ "a" : null, "b" : null, "result" : false }
{ "a" : undefined, "b" : 2, "result" : false }
{ "a" : 10, "b" : undefined, "result" : false }
{ "a" : undefined, "b" : undefined, "result" : false }
Qui, ho semplicemente usato il campo come espressione. Come previsto, hanno tutti restituito false .
Ecco come appare quando applichiamo un solo argomento:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 2, 3, 5, 8 ] } } },
{ $project: {
_id: 0,
a: 1,
result: { $and: [ "$a" ] } }
}
]
) Risultato:
{ "a" : 0, "result" : false }
{ "a" : 10, "result" : true }
{ "a" : null, "result" : false }
{ "a" : undefined, "result" : false }
Restituisce true quando il valore è 10 ma false in tutti gli altri casi.
Richiama $and senza argomenti
Quando viene invocato senza argomenti, $and l'operatore restituisce true .
Esempio:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 1 ] } } },
{ $project: {
_id: 0,
result: { $and: [ ] } }
}
]
) Risultato:
{ "result" : true }