Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So in the last lesson we looked at how you would create new records or new documents in our database.
In this lesson I want to show you how we can now read the data that's stored inside our database.
Again if we type in show collections you can see we have only a single collection called products but
inside products we can find all of the data. Heading back to our documentation
you can see that in order to read data from our database we can use a method called find. Following the
same format
we say db. the name of the collection .method.ind.
And inside the find method we pass in a query criteria.
In this case they're looking to find data within the user's collection where the age is greater than
18.
And if we click on this method then we get taken to a detailed page on this find method. And we've got
the definition, behavior and examples all written out in this very long document.
You can see the way that you use it is you pass in two parameters.
One is query and one is projection and they're both optional.
So you can simply write db.collection.find with empty parentheses and you will find everything.
So let's try that.
Let's say db.products.find and then we'll leave the parentheses empty.
And now if I hit enter I'll bring up all the documents that are inside my product collection and that is
my pen and my pencil.
But if you wanted to make a specific query for a piece of data in your collection then you can add a
query in there. And you can use query operators to narrow down on the data that you're going to get back.
And the query operators include things such as greater than less than or less than or equal.
And you can combine your queries using the logical operators and you have a very fine level of control
in terms of what data you want to read from your database.
For example if we were to query our database for a specific record, we could say something like db.
products to look inside our products collection,
and we're going to use the find method and inside the find method we're going to specify our query. And
we're going to use again that key value pair structure.
So the query that we have is find me all of the documents where the name field is equal to pencil.
And now if I hit enter you can see that it only brings me the data where this name field matches my
query criteria which is pencil.
And now I can read specific documents inside my collection or inside my database through the use of
a specific query.
Another query I could have say for example if I had loads of products and I only wanted to find the
ones that were priced above $1, then I could say db.products.find
and inside the parentheses I'm going to again add a set of curly braces and specify my key value pair.
So in this case I'm looking to find the documents where the price is greater than so $
gt: 1. Here I'm saying my query is based around the price of my records and I'm looking for
the ones where the price is greater than 1.0.
So we know we've got pencil that's priced at 0.8 and pen that's priced price one 1.2.
But if I run this code I only get back my pen document which has a price that is greater than 1.0.
You can get really fancy with the queries that you make to using the find method.
And you can drill down to the exact piece of data that you want from your database.
If we look back at the find method, you can see that there's a second parameter which is called projection.
And this again is optional which is why even though we didn't include it in our code it didn't really
matter.
You don't have to. But you can include it if you wanted to specify the fields to return.
If we head back to that CRUD documentation you can see that for this particular query they're looking
for documents inside the users collection where the age field is greater than 18
and their projection or the fields that they want to return are the name and the address.
So 1 means true and 0 means false.
So in our case for example we could say db.products.find and we're going to find all the
records that have a id of 1.
But then in our second parameter we're going to put in our projection,
so the field that we want back. And we only want the name. If we close off this method and hit enter then
you can see we get back our document with the name and the id.
Now whenever you use find, id always comes back by default. But you can use the projection to set it to
be false
so you can say for example id as 0
and this will only give you the name. But the second parameter inside this find method is a way for us
to specify which fields in the data do we want back.
And in this case we only want the name and nothing else.
In this case we want the name as well as the id which is included by default
and if we don't specify one, we get all the fields in that record.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.