So in the last lesson we looked at how you would create new records or new documents in our database. 1
In this lesson I want to show you how we can now read the data that's stored inside our database. 2
Again if we type in show collections you can see we have only a single collection called products but 3
inside products we can find all of the data. Heading back to our documentation 4
you can see that in order to read data from our database we can use a method called find. Following the 5
same format 6
we say db. the name of the collection .method.ind. 7
And inside the find method we pass in a query criteria. 8
In this case they're looking to find data within the user's collection where the age is greater than 9
18. 10
And if we click on this method then we get taken to a detailed page on this find method. And we've got 11
the definition, behavior and examples all written out in this very long document. 12
You can see the way that you use it is you pass in two parameters. 13
One is query and one is projection and they're both optional. 14
So you can simply write db.collection.find with empty parentheses and you will find everything. 15
So let's try that. 16
Let's say db.products.find and then we'll leave the parentheses empty. 17
And now if I hit enter I'll bring up all the documents that are inside my product collection and that is 18
my pen and my pencil. 19
But if you wanted to make a specific query for a piece of data in your collection then you can add a 20
query in there. And you can use query operators to narrow down on the data that you're going to get back. 21
And the query operators include things such as greater than less than or less than or equal. 22
And you can combine your queries using the logical operators and you have a very fine level of control 23
in terms of what data you want to read from your database. 24
For example if we were to query our database for a specific record, we could say something like db. 25
products to look inside our products collection, 26
and we're going to use the find method and inside the find method we're going to specify our query. And 27
we're going to use again that key value pair structure. 28
So the query that we have is find me all of the documents where the name field is equal to pencil. 29
And now if I hit enter you can see that it only brings me the data where this name field matches my 30
query criteria which is pencil. 31
And now I can read specific documents inside my collection or inside my database through the use of 32
a specific query. 33
Another query I could have say for example if I had loads of products and I only wanted to find the 34
ones that were priced above $1, then I could say db.products.find 35
and inside the parentheses I'm going to again add a set of curly braces and specify my key value pair. 36
So in this case I'm looking to find the documents where the price is greater than so $ 37
gt: 1. Here I'm saying my query is based around the price of my records and I'm looking for 38
the ones where the price is greater than 1.0. 39
So we know we've got pencil that's priced at 0.8 and pen that's priced price one 1.2. 40
But if I run this code I only get back my pen document which has a price that is greater than 1.0. 41
You can get really fancy with the queries that you make to using the find method. 42
And you can drill down to the exact piece of data that you want from your database. 43
If we look back at the find method, you can see that there's a second parameter which is called projection. 44
And this again is optional which is why even though we didn't include it in our code it didn't really 45
matter. 46
You don't have to. But you can include it if you wanted to specify the fields to return. 47
If we head back to that CRUD documentation you can see that for this particular query they're looking 48
for documents inside the users collection where the age field is greater than 18 49
and their projection or the fields that they want to return are the name and the address. 50
So 1 means true and 0 means false. 51
So in our case for example we could say db.products.find and we're going to find all the 52
records that have a id of 1. 53
But then in our second parameter we're going to put in our projection, 54
so the field that we want back. And we only want the name. If we close off this method and hit enter then 55
you can see we get back our document with the name and the id. 56
Now whenever you use find, id always comes back by default. But you can use the projection to set it to 57
be false 58
so you can say for example id as 0 59
and this will only give you the name. But the second parameter inside this find method is a way for us 60
to specify which fields in the data do we want back. 61
And in this case we only want the name and nothing else. 62
In this case we want the name as well as the id which is included by default 63
and if we don't specify one, we get all the fields in that record.