Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,390 --> 00:00:07,310
So in the last lesson we looked at how you would create new records or new documents in our database.
2
00:00:07,320 --> 00:00:13,740
In this lesson I want to show you how we can now read the data that's stored inside our database.
3
00:00:13,770 --> 00:00:20,700
Again if we type in show collections you can see we have only a single collection called products but
4
00:00:20,880 --> 00:00:27,640
inside products we can find all of the data. Heading back to our documentation
5
00:00:27,720 --> 00:00:34,890
you can see that in order to read data from our database we can use a method called find. Following the
6
00:00:34,890 --> 00:00:35,600
same format
7
00:00:35,610 --> 00:00:41,260
we say db. the name of the collection .method.ind.
8
00:00:41,610 --> 00:00:46,860
And inside the find method we pass in a query criteria.
9
00:00:46,860 --> 00:00:54,060
In this case they're looking to find data within the user's collection where the age is greater than
10
00:00:54,090 --> 00:00:55,260
18.
11
00:00:55,860 --> 00:01:03,360
And if we click on this method then we get taken to a detailed page on this find method. And we've got
12
00:01:03,360 --> 00:01:08,960
the definition, behavior and examples all written out in this very long document.
13
00:01:09,000 --> 00:01:12,510
You can see the way that you use it is you pass in two parameters.
14
00:01:12,510 --> 00:01:16,840
One is query and one is projection and they're both optional.
15
00:01:16,950 --> 00:01:24,810
So you can simply write db.collection.find with empty parentheses and you will find everything.
16
00:01:24,810 --> 00:01:26,390
So let's try that.
17
00:01:26,880 --> 00:01:34,530
Let's say db.products.find and then we'll leave the parentheses empty.
18
00:01:34,860 --> 00:01:42,600
And now if I hit enter I'll bring up all the documents that are inside my product collection and that is
19
00:01:42,600 --> 00:01:45,940
my pen and my pencil.
20
00:01:46,020 --> 00:01:54,060
But if you wanted to make a specific query for a piece of data in your collection then you can add a
21
00:01:54,060 --> 00:02:01,560
query in there. And you can use query operators to narrow down on the data that you're going to get back.
22
00:02:02,990 --> 00:02:11,920
And the query operators include things such as greater than less than or less than or equal.
23
00:02:12,140 --> 00:02:19,250
And you can combine your queries using the logical operators and you have a very fine level of control
24
00:02:19,280 --> 00:02:22,810
in terms of what data you want to read from your database.
25
00:02:23,980 --> 00:02:31,210
For example if we were to query our database for a specific record, we could say something like db.
26
00:02:31,570 --> 00:02:34,780
products to look inside our products collection,
27
00:02:35,080 --> 00:02:42,790
and we're going to use the find method and inside the find method we're going to specify our query. And
28
00:02:42,790 --> 00:02:46,320
we're going to use again that key value pair structure.
29
00:02:46,330 --> 00:02:55,540
So the query that we have is find me all of the documents where the name field is equal to pencil.
30
00:02:55,720 --> 00:03:03,520
And now if I hit enter you can see that it only brings me the data where this name field matches my
31
00:03:03,520 --> 00:03:05,960
query criteria which is pencil.
32
00:03:06,250 --> 00:03:13,810
And now I can read specific documents inside my collection or inside my database through the use of
33
00:03:13,870 --> 00:03:15,630
a specific query.
34
00:03:15,970 --> 00:03:21,520
Another query I could have say for example if I had loads of products and I only wanted to find the
35
00:03:21,520 --> 00:03:28,250
ones that were priced above $1, then I could say db.products.find
36
00:03:28,870 --> 00:03:37,060
and inside the parentheses I'm going to again add a set of curly braces and specify my key value pair.
37
00:03:37,270 --> 00:03:46,560
So in this case I'm looking to find the documents where the price is greater than so $
38
00:03:46,610 --> 00:03:57,190
gt: 1. Here I'm saying my query is based around the price of my records and I'm looking for
39
00:03:57,190 --> 00:04:01,500
the ones where the price is greater than 1.0.
40
00:04:01,750 --> 00:04:06,830
So we know we've got pencil that's priced at 0.8 and pen that's priced price one 1.2.
41
00:04:07,000 --> 00:04:14,960
But if I run this code I only get back my pen document which has a price that is greater than 1.0.
42
00:04:15,430 --> 00:04:22,060
You can get really fancy with the queries that you make to using the find method.
43
00:04:22,210 --> 00:04:28,650
And you can drill down to the exact piece of data that you want from your database.
44
00:04:28,690 --> 00:04:34,670
If we look back at the find method, you can see that there's a second parameter which is called projection.
45
00:04:34,810 --> 00:04:40,660
And this again is optional which is why even though we didn't include it in our code it didn't really
46
00:04:40,660 --> 00:04:41,250
matter.
47
00:04:41,320 --> 00:04:49,240
You don't have to. But you can include it if you wanted to specify the fields to return.
48
00:04:49,240 --> 00:04:56,590
If we head back to that CRUD documentation you can see that for this particular query they're looking
49
00:04:56,590 --> 00:05:03,140
for documents inside the users collection where the age field is greater than 18
50
00:05:03,370 --> 00:05:08,920
and their projection or the fields that they want to return are the name and the address.
51
00:05:08,920 --> 00:05:12,590
So 1 means true and 0 means false.
52
00:05:12,610 --> 00:05:19,090
So in our case for example we could say db.products.find and we're going to find all the
53
00:05:19,090 --> 00:05:24,160
records that have a id of 1.
54
00:05:24,350 --> 00:05:28,090
But then in our second parameter we're going to put in our projection,
55
00:05:28,130 --> 00:05:37,150
so the field that we want back. And we only want the name. If we close off this method and hit enter then
56
00:05:37,150 --> 00:05:42,580
you can see we get back our document with the name and the id.
57
00:05:42,580 --> 00:05:50,560
Now whenever you use find, id always comes back by default. But you can use the projection to set it to
58
00:05:50,560 --> 00:05:51,570
be false
59
00:05:51,610 --> 00:05:55,050
so you can say for example id as 0
60
00:05:55,450 --> 00:06:02,890
and this will only give you the name. But the second parameter inside this find method is a way for us
61
00:06:02,890 --> 00:06:07,330
to specify which fields in the data do we want back.
62
00:06:07,360 --> 00:06:10,680
And in this case we only want the name and nothing else.
63
00:06:10,750 --> 00:06:15,490
In this case we want the name as well as the id which is included by default
64
00:06:15,490 --> 00:06:19,380
and if we don't specify one, we get all the fields in that record.
7202
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.