WEBVTT 0 00:00.390 --> 00:07.310 So in the last lesson we looked at how you would create new records or new documents in our database. 1 00:07.320 --> 00:13.740 In this lesson I want to show you how we can now read the data that's stored inside our database. 2 00:13.770 --> 00:20.700 Again if we type in show collections you can see we have only a single collection called products but 3 00:20.880 --> 00:27.640 inside products we can find all of the data. Heading back to our documentation 4 00:27.720 --> 00:34.890 you can see that in order to read data from our database we can use a method called find. Following the 5 00:34.890 --> 00:35.600 same format 6 00:35.610 --> 00:41.260 we say db. the name of the collection .method.ind. 7 00:41.610 --> 00:46.860 And inside the find method we pass in a query criteria. 8 00:46.860 --> 00:54.060 In this case they're looking to find data within the user's collection where the age is greater than 9 00:54.090 --> 00:55.260 18. 10 00:55.860 --> 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 11 01:03.360 --> 01:08.960 the definition, behavior and examples all written out in this very long document. 12 01:09.000 --> 01:12.510 You can see the way that you use it is you pass in two parameters. 13 01:12.510 --> 01:16.840 One is query and one is projection and they're both optional. 14 01:16.950 --> 01:24.810 So you can simply write db.collection.find with empty parentheses and you will find everything. 15 01:24.810 --> 01:26.390 So let's try that. 16 01:26.880 --> 01:34.530 Let's say db.products.find and then we'll leave the parentheses empty. 17 01:34.860 --> 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 18 01:42.600 --> 01:45.940 my pen and my pencil. 19 01:46.020 --> 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 20 01:54.060 --> 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. 21 02:02.990 --> 02:11.920 And the query operators include things such as greater than less than or less than or equal. 22 02:12.140 --> 02:19.250 And you can combine your queries using the logical operators and you have a very fine level of control 23 02:19.280 --> 02:22.810 in terms of what data you want to read from your database. 24 02:23.980 --> 02:31.210 For example if we were to query our database for a specific record, we could say something like db. 25 02:31.570 --> 02:34.780 products to look inside our products collection, 26 02:35.080 --> 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 27 02:42.790 --> 02:46.320 we're going to use again that key value pair structure. 28 02:46.330 --> 02:55.540 So the query that we have is find me all of the documents where the name field is equal to pencil. 29 02:55.720 --> 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 30 03:03.520 --> 03:05.960 query criteria which is pencil. 31 03:06.250 --> 03:13.810 And now I can read specific documents inside my collection or inside my database through the use of 32 03:13.870 --> 03:15.630 a specific query. 33 03:15.970 --> 03:21.520 Another query I could have say for example if I had loads of products and I only wanted to find the 34 03:21.520 --> 03:28.250 ones that were priced above $1, then I could say db.products.find 35 03:28.870 --> 03:37.060 and inside the parentheses I'm going to again add a set of curly braces and specify my key value pair. 36 03:37.270 --> 03:46.560 So in this case I'm looking to find the documents where the price is greater than so $ 37 03:46.610 --> 03:57.190 gt: 1. Here I'm saying my query is based around the price of my records and I'm looking for 38 03:57.190 --> 04:01.500 the ones where the price is greater than 1.0. 39 04:01.750 --> 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. 40 04:07.000 --> 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. 41 04:15.430 --> 04:22.060 You can get really fancy with the queries that you make to using the find method. 42 04:22.210 --> 04:28.650 And you can drill down to the exact piece of data that you want from your database. 43 04:28.690 --> 04:34.670 If we look back at the find method, you can see that there's a second parameter which is called projection. 44 04:34.810 --> 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 45 04:40.660 --> 04:41.250 matter. 46 04:41.320 --> 04:49.240 You don't have to. But you can include it if you wanted to specify the fields to return. 47 04:49.240 --> 04:56.590 If we head back to that CRUD documentation you can see that for this particular query they're looking 48 04:56.590 --> 05:03.140 for documents inside the users collection where the age field is greater than 18 49 05:03.370 --> 05:08.920 and their projection or the fields that they want to return are the name and the address. 50 05:08.920 --> 05:12.590 So 1 means true and 0 means false. 51 05:12.610 --> 05:19.090 So in our case for example we could say db.products.find and we're going to find all the 52 05:19.090 --> 05:24.160 records that have a id of 1. 53 05:24.350 --> 05:28.090 But then in our second parameter we're going to put in our projection, 54 05:28.130 --> 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 55 05:37.150 --> 05:42.580 you can see we get back our document with the name and the id. 56 05:42.580 --> 05:50.560 Now whenever you use find, id always comes back by default. But you can use the projection to set it to 57 05:50.560 --> 05:51.570 be false 58 05:51.610 --> 05:55.050 so you can say for example id as 0 59 05:55.450 --> 06:02.890 and this will only give you the name. But the second parameter inside this find method is a way for us 60 06:02.890 --> 06:07.330 to specify which fields in the data do we want back. 61 06:07.360 --> 06:10.680 And in this case we only want the name and nothing else. 62 06:10.750 --> 06:15.490 In this case we want the name as well as the id which is included by default 63 06:15.490 --> 06:19.380 and if we don't specify one, we get all the fields in that record.