1 00:00:02,170 --> 00:00:04,430 We get our flights in the database again 2 00:00:04,630 --> 00:00:09,730 and now let me dive a bit deeper into how we can find data. Thus far, 3 00:00:09,850 --> 00:00:14,850 we always used this way of finding data, find with no arguments. 4 00:00:15,010 --> 00:00:21,640 Now just as you can filter for specific records or documents when deleting or updating, you can also 5 00:00:21,670 --> 00:00:23,080 do so when finding. 6 00:00:23,350 --> 00:00:26,100 So I can access my flight data here, 7 00:00:26,170 --> 00:00:28,700 let me quickly clear this to make it a bit easier to read 8 00:00:29,800 --> 00:00:37,210 and instead of passing nothing, you can pass a document to it which will be interpreted as a filter for 9 00:00:37,420 --> 00:00:41,950 your collection and you can filter for anything that makes sense. 10 00:00:42,010 --> 00:00:48,400 You could filter for a name equal to Max but of course this returns nothing because in our collection, 11 00:00:48,400 --> 00:00:51,520 we get these two flights and there is no name field in there 12 00:00:51,730 --> 00:01:00,610 but what we can do of course is we can use flight data and find all flights that are let's say intercontinental, 13 00:01:00,910 --> 00:01:03,250 so we use that key here. 14 00:01:03,350 --> 00:01:09,770 We do so by adding a document, then the name and this can be wrapped by quotation marks but it doesn't 15 00:01:09,770 --> 00:01:10,670 have to 16 00:01:10,670 --> 00:01:16,880 and then let's say I want to find all flights where this is true, this would be a valid filter. If I now 17 00:01:17,930 --> 00:01:20,250 pretty print this which always is optional, 18 00:01:20,270 --> 00:01:23,280 just to make it easier to read, if I hit enter, 19 00:01:23,360 --> 00:01:28,640 now I get one flight and this is one flight which is an intercontinental flight. 20 00:01:28,640 --> 00:01:34,910 Of course, this is a very useful feature because in pretty much every application you're creating, 21 00:01:34,910 --> 00:01:41,540 you are not always interested in all your data but in a subset of the data which you need for that specific 22 00:01:41,540 --> 00:01:47,990 webpage you're showing to the user or that specific part of your mobile app. You're also not limited 23 00:01:48,080 --> 00:01:50,220 to simple queries like this, 24 00:01:50,240 --> 00:01:56,990 we could also find all flights with a distance greater than ten thousand kilometers and for this, we 25 00:01:56,990 --> 00:01:59,040 just need to modify the syntax a bit more. 26 00:01:59,150 --> 00:02:03,420 It's easy to look for equal values, distance 12000 like this, 27 00:02:03,530 --> 00:02:08,950 that would be simple and it gives us back this one flight here, just not pretty printed 28 00:02:08,960 --> 00:02:12,960 but if we want to have a greater than check, we can do that too. 29 00:02:13,310 --> 00:02:17,020 And there we just need to pass another document or object, 30 00:02:17,180 --> 00:02:19,150 I'll use these words interchangeably, 31 00:02:19,310 --> 00:02:22,760 we need to pass another object as a value for distance 32 00:02:22,850 --> 00:02:29,060 and here we again use one of these special operators provided by mongodb, $gt which 33 00:02:29,060 --> 00:02:31,900 stands for greater than and no worries, 34 00:02:31,920 --> 00:02:39,200 you don't need to learn them by heart right now. Gt does exist, $gt and that means greater than 35 00:02:39,380 --> 00:02:42,980 and then you enter the value which you want to have as a greater than value. 36 00:02:42,980 --> 00:02:44,990 So here we could enter 10000 37 00:02:45,320 --> 00:02:47,730 and let's also pretty print this to make 38 00:02:47,830 --> 00:02:53,460 it a bit easier to read and we get this one flight which has a distance greater than 10000. 39 00:02:53,600 --> 00:03:01,070 The other flight had a distance of 950 and therefore if I modify this to look for all flights with 40 00:03:01,070 --> 00:03:05,720 a distance greater than 900, we actually get back both flights, 41 00:03:05,720 --> 00:03:09,110 so this is of course another very useful feature. 42 00:03:09,110 --> 00:03:15,790 Now let me close this lecture or end this lecture by showing you findOne with this example. 43 00:03:15,820 --> 00:03:22,310 So I'm looking for flights with a distance greater than 900 kilometers, if I switch it from find to findOne 44 00:03:22,310 --> 00:03:30,890 and I remove pretty here because pretty is not supported on findOne, I get back only one flight, the 45 00:03:30,890 --> 00:03:36,260 first flight that matched my criteria and that makes sense because findOne only gives me back one 46 00:03:36,440 --> 00:03:37,100 element. 47 00:03:37,100 --> 00:03:44,560 So if I want to find all, I should use find, if I am only interested in the first matching element, findOne is the 48 00:03:44,570 --> 00:03:45,440 solution. 49 00:03:45,450 --> 00:03:51,290 Important is it will still respect that filter but then not look for all matching elements but only 50 00:03:51,290 --> 00:03:52,810 the first matching element.