1 00:00:02,170 --> 00:00:03,620 So I'm back in the shell 2 00:00:03,760 --> 00:00:08,740 and the important thing is we'll work with movies which we imported in the last course module, 3 00:00:08,740 --> 00:00:10,800 so make sure you got that data there 4 00:00:11,050 --> 00:00:17,280 and if I can quickly findOne, that already is our first read method. findOne does what the name suggests, 5 00:00:17,290 --> 00:00:25,750 it finds exactly one document, you can pass a filter to it and we learn about filters later to define 6 00:00:25,750 --> 00:00:27,700 which one document that should be 7 00:00:27,790 --> 00:00:31,450 but then it will always find the first matching document, 8 00:00:31,540 --> 00:00:35,860 if you specify nothing, it will look through all documents and simply give you back the first document 9 00:00:35,860 --> 00:00:36,970 in your collection, 10 00:00:36,970 --> 00:00:42,400 if you specified some filter, like age 30 which will not work here because we have no age field but if 11 00:00:42,400 --> 00:00:47,210 you specify something like this, it would give you the first document where this condition is met, 12 00:00:47,410 --> 00:00:53,350 so empty curly braces or no curly braces at all will simply give you back the first document. 13 00:00:53,350 --> 00:00:58,600 Please note that this also does not give you back a cursor but only one document which it instantly 14 00:00:58,600 --> 00:00:59,520 loads. 15 00:00:59,560 --> 00:01:03,130 Now this is the shape of the data in our movies collection, 16 00:01:03,130 --> 00:01:08,170 this is data about TV series and maybe we should have named it series therefore but now it's movies 17 00:01:08,760 --> 00:01:09,160 and there you see 18 00:01:09,160 --> 00:01:13,100 we've got a couple of fields, like the name, which type it is, language, 19 00:01:13,180 --> 00:01:20,440 some genres, if it's still running or already ended, the runtime when it premiered 20 00:01:20,460 --> 00:01:23,860 and well that should be all pretty self-explanatory, 21 00:01:24,040 --> 00:01:28,220 there is a bunch of data for us to play around with and that is exactly what we'll do. 22 00:01:28,540 --> 00:01:30,820 So this was findOne, 23 00:01:31,030 --> 00:01:39,550 now let's say we want to find one document but we want to find one different document. For that, 24 00:01:39,580 --> 00:01:47,760 let's first of all have a look at the alternative to findOne which is find, now find like this will 25 00:01:47,770 --> 00:01:52,360 give us back a cursor as you learned and we'll care about cursors later, 26 00:01:52,360 --> 00:01:57,490 for now let's just accept it, pretty is a method we can change to get a prettier output. 27 00:01:57,490 --> 00:02:03,040 Now this theoretically gives us all documents but since it gives us a cursor in the end, it does not 28 00:02:03,040 --> 00:02:08,470 give us all documents to avoid querying the database or a collection for all documents, in the shell, 29 00:02:08,470 --> 00:02:14,790 we just get back the first 20 documents. So with find, we see there's a bunch of stuff in there 30 00:02:15,010 --> 00:02:22,000 and for example, there is The Last Ship series and why don't we tried to find this with either find 31 00:02:22,000 --> 00:02:26,090 one or find. Well to do this, 32 00:02:26,200 --> 00:02:34,570 we will need a filter and we specify a filter on both findOne and find, it doesn't matter, 33 00:02:34,570 --> 00:02:40,240 they work in exactly the same way regarding all these options you learned about by passing a document 34 00:02:40,570 --> 00:02:43,760 to the method, so that again is the same for findOne and find, it 35 00:02:43,760 --> 00:02:44,800 doesn't matter, 36 00:02:44,800 --> 00:02:49,390 the only thing that does change of course is that findOne will give us back one document that meets 37 00:02:49,390 --> 00:02:50,160 that criteria, 38 00:02:50,200 --> 00:02:54,160 find will give us back all documents that meet that criteria. 39 00:02:54,160 --> 00:02:57,450 Now here I want to check for the name, 40 00:02:57,470 --> 00:03:05,820 so for the name field to be equal to The Last Ship, we can do this by specifying the key here, name, 41 00:03:05,840 --> 00:03:10,880 so the field where we want to look into and then the value and by default, 42 00:03:11,000 --> 00:03:15,490 if you specify it like this, so could just key value, mongodb 43 00:03:15,500 --> 00:03:17,990 will look for equality. 44 00:03:18,090 --> 00:03:23,100 So if now hit enter, we get all documents where the name is The Last Ship which happens to only be 45 00:03:23,100 --> 00:03:24,840 one document, 46 00:03:24,870 --> 00:03:32,930 we of course would get more documents if we used a field where the value would be repeating, 47 00:03:32,940 --> 00:03:45,860 so for example if we use runtime 60 by quickly changing that to runtime and then that is a number, so 60, 48 00:03:45,970 --> 00:03:48,220 then you see these are way more documents, 49 00:03:48,220 --> 00:03:49,570 obviously it's a wall of text, 50 00:03:49,570 --> 00:03:54,940 we could use pretty to make it easier to read but the main thing is these are multiple documents. Now 51 00:03:54,940 --> 00:04:00,850 that also allows me to highlight the difference with findOne, findOne only gives us back one document, 52 00:04:00,850 --> 00:04:05,710 the first document that meets that criteria which is Glee and not The Last Ship because that happens 53 00:04:05,710 --> 00:04:07,060 to be the first document 54 00:04:07,180 --> 00:04:14,230 having the right runtime. So this is findOne and find and how you pass a filter by passing a document 55 00:04:14,440 --> 00:04:18,170 as an argument and that document describes your query selector, 56 00:04:18,190 --> 00:04:24,370 so your filtering criteria you could say. Here we use equality but of course as I already mentioned, there 57 00:04:24,370 --> 00:04:27,850 are way more ways of filtering and way more operators. 58 00:04:27,850 --> 00:04:30,280 So let's dive into these operators.