MongoDB fornisce il db.collection.find() metodo per interrogare i documenti all'interno di una raccolta.
Il db.collection.find() seleziona i documenti in una raccolta e riporta un cursore sui documenti selezionati.
Restituisci tutti i documenti
Questo esempio restituisce tutti i documenti di musicisti collezione:
db.musicians.find()
Risultato:
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
Restituisce tutti i documenti perché non abbiamo passato alcun parametro come criterio di filtro.
La query precedente è una versione ridotta di db.musicians.find( {} ) . Nella query precedente, abbiamo omesso le parentesi graffe {} . Questo è perfettamente valido quando si lavora con MongoDB.
Aggiungi criteri di filtraggio
Puoi filtrare i risultati fornendo solo i criteri che ti interessano.
Ad esempio, se siamo interessati solo ai Deep Purple degli artisti collezione:
db.artists.find({ artistname : "Deep Purple" }) Risultato:
{ "_id" : ObjectId("5781f85d48ef8c6b3ffb0150"), "artistname" : "Deep Purple", "albums" : [ { "album" : "Machine Head", "year" : 1972, "genre" : "Rock" }, { "album" : "Stormbringer", "year" : 1974, "genre" : "Rock" } ] }
Formatta i risultati
Potresti trovare i risultati di cui sopra un po' difficili da leggere. Il documento viene restituito come una lunga riga di testo.
Puoi usare pretty() metodo per formattare i risultati in modo che siano un po' più facili da leggere.
Basta aggiungere pretty() fino alla fine, così:
db.artists.find({ artistname : "Deep Purple" }).pretty() Risultato:
{
"_id" : ObjectId("5781f85d48ef8c6b3ffb0150"),
"artistname" : "Deep Purple",
"albums" : [
{
"album" : "Machine Head",
"year" : 1972,
"genre" : "Rock"
},
{
"album" : "Stormbringer",
"year" : 1974,
"genre" : "Rock"
}
]
}
Più opzioni di filtro
Ecco altri modi per filtrare i risultati.
Specifica AND Condizioni
È possibile specificare che devono essere restituiti solo i documenti contenenti due o più valori specificati.
In questo esempio, specifichiamo che solo i musicisti che suonano la batteria e dove nati prima del 1950 devono essere restituiti. Verranno restituiti solo i documenti che soddisfano entrambi i criteri.
db.musicians.find( { instrument: "Drums", born: { $lt: 1950 } } ) Risultato:
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
Specifica OR Condizioni
È inoltre possibile specificare che l'uno o l'altro valore debba essere true. Finché una delle condizioni è soddisfatta, il documento verrà restituito.
In questo esempio, vogliamo documenti che contengano musicisti che suonano la batteria o che sono nati prima del 1950.
db.musicians.find(
{
$or: [ { instrument: "Drums" }, { born: { $lt: 1950 } } ]
}
) Risultato:
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
Il $in Operatore
Il $in operatore consente di fornire un elenco di valori. Se un documento contiene uno di questi valori, verrà restituito.
Utilizzando il seguente esempio, stiamo cercando tutti i musicisti che sono alla voce o suonano la chitarra.
db.musicians.find( { instrument: { $in: [ "Vocals", "Guitar" ] } } ) Risultato
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
Interroga una matrice di documenti
Questo esempio interroga una matrice di documenti. Trova gli album pubblicati dopo il 2000.
db.artists.find(
{
albums: {
$elemMatch: {
year: { $gt: 2000 }
}
}
}
).pretty() Risultato:
{
"_id" : ObjectId("578217c248ef8c6b3ffb015a"),
"artistname" : "Robben Ford",
"albums" : [
{
"album" : "Bringing it Back Home",
"year" : 2013,
"genre" : "Blues"
},
{
"album" : "Talk to Your Daughter",
"year" : 1988,
"genre" : "Blues"
}
]
}
{
"_id" : ObjectId("578217c248ef8c6b3ffb015b"),
"artistname" : "Snoop Dogg",
"albums" : [
{
"album" : "Tha Doggfather",
"year" : 1996,
"genre" : "Rap"
},
{
"album" : "Reincarnated",
"year" : 2013,
"genre" : "Reggae"
}
]
}
Noterai che questi risultati contengono anche album precedenti al 2000. Questo è corretto:è il modo in cui funzionano i database orientati ai documenti. Qualsiasi query restituirà l'intero documento (ma solo i documenti che soddisfano i criteri specificati).
Il db.collection.findOne() Metodo
Puoi usare db.collection.findOne() metodo per restituire un documento che soddisfa i criteri di query specificati.
Se più documenti soddisfano i criteri, viene restituito solo il primo, come determinato dall'ordine naturale dei documenti su disco.
Quindi cercare un'intera collezione come questa:
db.musicians.findOne( )
Restituirà un solo documento:
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
Se cambiamo il findOne() per find() così:
db.musicians.find( )
Vediamo che in realtà ci sono 8 documenti nella collezione:
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }