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
Now that we've seen how we can create new documents and new collections into our database using Mongoose
and MongoDB,
the next thing to look at is how do we read from our database?
How do we perform
what is the equivalent of find that we saw in the Mongo shell?
Well it's also pretty simple and it's a lot simpler than this.
All we have to do is tap into our fruit model which you can effectively see as kind of your collection
right?
This is going to be all about fruits and we're going to call the Find function on it.
Now this find function can accept a callback. And that callback has two parameters.
The first one is error and the second one is whatever it finds back.
So we can call it anything we want. But in this case given that we're searching through our fruits collection
to find everything because we're not filtering, we're not querying anything, then what we'll get back
is all of our fruits right? Inside this callback
we can check to see if there was an error and if so we'll log the error. But otherwise we'll simply just
log the fruits that we found from the find method.
So the full syntax looks a bit like this.
We tap into our fruits collection through the fruit model, we call the find function on it and then when
that's completed a callback gets triggered and we have the possibility of having an error if anything
went wrong with finding our fruits but otherwise we would get some results back which we've called fruit.
And if there were no errors then we simply just log all the results that we get back from our find function.
Now just before we run on app.js, unless you want to insert another three of these same fruits into
our fruits collection, it's a good idea to comment out this part where we call the insertMany function.
That way we won't end up with the same fruits inserted into our collection many many times or at least
every single time we run app.js.
So let's head over to our hyper terminal and let's hit CONTROL + C to exit.
And again run on node app.js.
So now you can see that it's read into our fruits database, which is what we asked it to do here,
and then it's performed that find function onto it.
And because there were no errors then it's logged all of the results that it found in that collection
which is basically all of our fruits. And these aren't living Javascript objects and they're all contained
inside an array.
So everything that we've learned so far about arrays and Javascript objects now applies to our data
in our database which means that we can tap into its properties by using the dot notation or pass it
around in two different methods and we can work with it in our app.
js.
So given that you know that this thing we get back called fruits is an array of fruit objects each
with their own properties such as name and rating
and each of those properties have associated values,
then here's a challenge for you.
I want you to use the for each loop that we learnt about previously in the EJS challenge module
and I want you to loop through the array of fruits and only log their names,
so the names of the fruit.
So if you were successful then inside the console here you should be able to see just apple, kiwi, orange
banana without the rest of this data.
Pause the video now and try to complete the challenge.
Okay. So we know that inside here we have access to an array called fruits.
How can we loop through just any other array?
Well, we would tap into the array, fruits, and then we would call the forEach method onto it.
And this forEach method accepts a callback
and inside that callback we have a single parameter which is going to be each individual object inside
the array.
So when we loop through it
we're going to pick out each and every fruit out of the array.
So we'll just call it fruit
in that case, the singular form.
And now inside our callback we get two console log
each fruit and we get to tap into its name property through the use of the dot name.
Now through this little very short bit of code, we loop through our fruits array that we got back from
our database
and for each fruit, we will console log the fruit's name.
There's a lot of singular and plurals going on around here.
So you have to make sure you know which one you're using when.
Let's go ahead and delete that previous console log
where we logged the entire fruits array.
And let's run our app.js to see it in action.
So let's again close our database connection with CONTROL + C and then run node app.
js.
And you can see we now loop through our fruits array and we print out each and every name property.
So we end up with apple, kiwi, orange, banana.
And so you can imagine that inside our app.js now we have the entire flexibility of working with Javascript
arrays and Javascript native objects to tap into their properties, to pass them around into functions
and use them wherever we need them.
Now at the moment, you might have noticed that we have to keep closing our database connection with CONTROL +
C. This is because inside our app.
js, we're not closing that database connection. We're keeping it open at the end.
We open that connection when we call mongoose.connect.
But it's also good practice to close the connection to our database
once we're done with it in our app. Inside the method where we perform the last action we want to do
with our database which in this case is finding fruits inside the database,
then once it's completed and the callback is being called and we have no errors then what we can do
is we can say "mongoose.connection.close" so we call the close method on our mongoose collection.
And now if I rerun my app
so for the last time use CONTROL + C to exit out of our database connection, run node app.
js and you can see that once it's done performing the task I asked it to
which is to find all of the fruits inside the fruit collection then log its names through this forEach
loop then it's close the connection and I don't have to use CONTROL + C anymore.
So this is good practice whenever you're working with MongoDB databases and Mongoose. Close the connection
once you've done.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.