L'operatore di query per elementi $exists di MongoDB ti permette di trovare i documenti che contengono un determinato campo. Puoi anche individuare con la stessa facilità i documenti che non lo contengono.
Inoltre si combina bene con altri operatori, come $nin, consentendoti di trovare documenti in cui un campo è presente ma non ha un valore specifico.
Esempio
Immagina una collezione chiamata cats con questi documenti:
{ "_id" : 1, "name" : "Scratch", "born" : "March, 2020" } { "_id" : 2, "name" : "Meow", "weight" : 30 } { "_id" : 3, "name" : "Fluffy", "height" : 15 } { "_id" : 4, "name" : "Sox", "weight" : 40 } { "_id" : 5, "name" : null, "weight" : 20 } { "_id" : 6, "height" : 20, "born" : ISODate("2021-01-03T23:30:15.123Z") }Nota quanto siano incoerenti questi documenti. Alcuni hanno un campo weight, altri un campo height e un paio includono born.
L'operatore $exists è utile per restituire i documenti che hanno un determinato campo.
Esempio:
db.cats.find( { weight: { $exists: true } } )Risultato:
{ "_id" : 2, "name" : "Meow", "weight" : 30 } { "_id" : 4, "name" : "Sox", "weight" : 40 } { "_id" : 5, "name" : null, "weight" : 20 }Solo i documenti con un campo weight vengono inclusi.
Campi con valore null
Da tenere presente: $exists considera esistente un campo impostato su null. Non fa distinzione tra null e valori non nulli.
Esempio:
db.cats.find( { name: { $exists: true } } )Risultato:
{ "_id" : 1, "name" : "Scratch", "born" : "March, 2020" } { "_id" : 2, "name" : "Meow", "weight" : 30 } { "_id" : 3, "name" : "Fluffy", "height" : 15 } { "_id" : 4, "name" : "Sox", "weight" : 40 } { "_id" : 5, "name" : null, "weight" : 20 }Il documento 5 compare anche se il suo campo name è null.
Verificare l'esistenza senza un valore specifico
Abbina $exists ad altri operatori per restituire documenti che hanno il campo ma con un valore diverso da quello indicato.
Esempio:
db.cats.find( { weight: { $exists: true, $nin: [20,30] } } )Risultato:
{ "_id" : 4, "name" : "Sox", "weight" : 40 }È un risultato diverso da quello che darebbe $nin da solo. Per confronto, ecco $nin senza $exists:
db.cats.find( { weight: { $nin: [20,30] } } )Risultato:
{ "_id" : 1, "name" : "Scratch", "born" : "March, 2020" } { "_id" : 3, "name" : "Fluffy", "height" : 15 } { "_id" : 4, "name" : "Sox", "weight" : 40 } { "_id" : 6, "height" : 20, "born" : ISODate("2021-01-03T23:30:15.123Z") }Documenti privi di un campo
Impostando $exists: false ottieni i documenti che non contengono il campo indicato.
Esempio:
db.cats.find( { name: { $exists: false } } )Risultato:
{ "_id" : 6, "height" : 20, "born" : ISODate("2021-01-03T23:30:15.123Z") }Nella collezione c'è un solo documento senza campo name.
Controllare più campi contemporaneamente
Per verificare più campi, separa ogni condizione con una virgola.
Esempio:
db.cats.find( { name: { $exists: true }, height: { $exists: true } } )Risultato:
{ "_id" : 3, "name" : "Fluffy", "height" : 15 }Questo restituisce tutti i documenti che contengono sia un campo name sia un campo height.