Now that we've seen how we can create new documents and new collections into our database using Mongoose 1
and MongoDB, 2
the next thing to look at is how do we read from our database? 3
How do we perform 4
what is the equivalent of find that we saw in the Mongo shell? 5
Well it's also pretty simple and it's a lot simpler than this. 6
All we have to do is tap into our fruit model which you can effectively see as kind of your collection 7
right? 8
This is going to be all about fruits and we're going to call the Find function on it. 9
Now this find function can accept a callback. And that callback has two parameters. 10
The first one is error and the second one is whatever it finds back. 11
So we can call it anything we want. But in this case given that we're searching through our fruits collection 12
to find everything because we're not filtering, we're not querying anything, then what we'll get back 13
is all of our fruits right? Inside this callback 14
we can check to see if there was an error and if so we'll log the error. But otherwise we'll simply just 15
log the fruits that we found from the find method. 16
So the full syntax looks a bit like this. 17
We tap into our fruits collection through the fruit model, we call the find function on it and then when 18
that's completed a callback gets triggered and we have the possibility of having an error if anything 19
went wrong with finding our fruits but otherwise we would get some results back which we've called fruit. 20
And if there were no errors then we simply just log all the results that we get back from our find function. 21
Now just before we run on app.js, unless you want to insert another three of these same fruits into 22
our fruits collection, it's a good idea to comment out this part where we call the insertMany function. 23
That way we won't end up with the same fruits inserted into our collection many many times or at least 24
every single time we run app.js. 25
So let's head over to our hyper terminal and let's hit CONTROL + C to exit. 26
And again run on node app.js. 27
So now you can see that it's read into our fruits database, which is what we asked it to do here, 28
and then it's performed that find function onto it. 29
And because there were no errors then it's logged all of the results that it found in that collection 30
which is basically all of our fruits. And these aren't living Javascript objects and they're all contained 31
inside an array. 32
So everything that we've learned so far about arrays and Javascript objects now applies to our data 33
in our database which means that we can tap into its properties by using the dot notation or pass it 34
around in two different methods and we can work with it in our app. 35
js. 36
So given that you know that this thing we get back called fruits is an array of fruit objects each 37
with their own properties such as name and rating 38
and each of those properties have associated values, 39
then here's a challenge for you. 40
I want you to use the for each loop that we learnt about previously in the EJS challenge module 41
and I want you to loop through the array of fruits and only log their names, 42
so the names of the fruit. 43
So if you were successful then inside the console here you should be able to see just apple, kiwi, orange 44
banana without the rest of this data. 45
Pause the video now and try to complete the challenge. 46
Okay. So we know that inside here we have access to an array called fruits. 47
How can we loop through just any other array? 48
Well, we would tap into the array, fruits, and then we would call the forEach method onto it. 49
And this forEach method accepts a callback 50
and inside that callback we have a single parameter which is going to be each individual object inside 51
the array. 52
So when we loop through it 53
we're going to pick out each and every fruit out of the array. 54
So we'll just call it fruit 55
in that case, the singular form. 56
And now inside our callback we get two console log 57
each fruit and we get to tap into its name property through the use of the dot name. 58
Now through this little very short bit of code, we loop through our fruits array that we got back from 59
our database 60
and for each fruit, we will console log the fruit's name. 61
There's a lot of singular and plurals going on around here. 62
So you have to make sure you know which one you're using when. 63
Let's go ahead and delete that previous console log 64
where we logged the entire fruits array. 65
And let's run our app.js to see it in action. 66
So let's again close our database connection with CONTROL + C and then run node app. 67
js. 68
And you can see we now loop through our fruits array and we print out each and every name property. 69
So we end up with apple, kiwi, orange, banana. 70
And so you can imagine that inside our app.js now we have the entire flexibility of working with Javascript 71
arrays and Javascript native objects to tap into their properties, to pass them around into functions 72
and use them wherever we need them. 73
Now at the moment, you might have noticed that we have to keep closing our database connection with CONTROL + 74
C. This is because inside our app. 75
js, we're not closing that database connection. We're keeping it open at the end. 76
We open that connection when we call mongoose.connect. 77
But it's also good practice to close the connection to our database 78
once we're done with it in our app. Inside the method where we perform the last action we want to do 79
with our database which in this case is finding fruits inside the database, 80
then once it's completed and the callback is being called and we have no errors then what we can do 81
is we can say "mongoose.connection.close" so we call the close method on our mongoose collection. 82
And now if I rerun my app 83
so for the last time use CONTROL + C to exit out of our database connection, run node app. 84
js and you can see that once it's done performing the task I asked it to 85
which is to find all of the fruits inside the fruit collection then log its names through this forEach 86
loop then it's close the connection and I don't have to use CONTROL + C anymore. 87
So this is good practice whenever you're working with MongoDB databases and Mongoose. Close the connection 88
once you've done.