1 00:00:02,210 --> 00:00:04,880 Now that we talked so much about relations, 2 00:00:04,940 --> 00:00:11,960 let me show you one useful operation mongodb offers for merging related documents that 3 00:00:11,960 --> 00:00:14,620 you split up by using the reference approach, 4 00:00:14,630 --> 00:00:18,050 so where you got no embedded documents and that is the lookup 5 00:00:18,110 --> 00:00:22,390 operator. Now the lookup operator is used like this, 6 00:00:22,430 --> 00:00:25,700 this uses the aggregate method which we haven't learned about, 7 00:00:25,700 --> 00:00:30,180 we got a whole module about the aggregation framework and I don't want to talk too much about it 8 00:00:30,290 --> 00:00:39,320 but lookup is essentially a helpful tool that allows you to fetch two related documents merged together 9 00:00:39,380 --> 00:00:43,850 in one document. in one step instead of having to do two steps 10 00:00:43,850 --> 00:00:50,360 and this mitigates some of the disadvantages of splitting your documents across collections because 11 00:00:50,360 --> 00:00:53,440 now you can at least merge them in one go. 12 00:00:53,450 --> 00:00:55,340 Let me show you how it works. 13 00:00:55,490 --> 00:00:58,690 Here we got our book author example still 14 00:00:58,740 --> 00:01:04,870 and now let's say I want to fetch all my books with the author data included, to do that 15 00:01:04,880 --> 00:01:09,800 I can use my books, my books collection and use the aggregate method, 16 00:01:09,830 --> 00:01:12,670 again this uses the so-called aggregation framework 17 00:01:12,710 --> 00:01:15,510 and we'll have a look at this later in the course in detail. 18 00:01:15,890 --> 00:01:21,280 There you pass an array because you can define multiple steps on how to aggregate your data, 19 00:01:21,320 --> 00:01:23,350 for now there is only one step I'm interested, 20 00:01:23,360 --> 00:01:29,470 a step is simply a document you pass to the array that is the lookup step, $lookup. That 21 00:01:29,480 --> 00:01:32,590 step is configured by passing a document as a value 22 00:01:32,720 --> 00:01:37,740 and here you need to define four things, I simply brought this into a new page to make it easier to read, 23 00:01:37,790 --> 00:01:40,370 so four things need to be defined here. 24 00:01:40,400 --> 00:01:42,230 The first one is the from thing 25 00:01:42,360 --> 00:01:49,480 and the question here is from which other collection do you want to relate documents? 26 00:01:49,490 --> 00:01:53,970 So here we're running aggregate on the books collection, 27 00:01:54,050 --> 00:02:00,660 so we want to pull in other documents from the authors collection, 28 00:02:00,680 --> 00:02:08,000 so that is the name of the collection where your related documents live in. You then define the local field, 29 00:02:08,040 --> 00:02:14,200 so in the collection we're running this on, so the books collection, where can the references to the other 30 00:02:14,310 --> 00:02:23,280 collection be found in? And for books, we stored that in the authors field, 31 00:02:26,850 --> 00:02:31,190 authors simply is an array of objectIds. 32 00:02:31,210 --> 00:02:36,660 Now we need to tell MongoDB ok I know where your keys are stored, 33 00:02:36,700 --> 00:02:40,540 which field are you relating to in your target collection, 34 00:02:40,540 --> 00:02:47,650 so in the author collection here, the authors collection and you define that with the foreign field key 35 00:02:47,850 --> 00:02:50,320 and there we know it's an _ID. 36 00:02:50,320 --> 00:02:56,700 Please note that all these values are enclosed in double quotation marks, authors, authors 37 00:02:56,830 --> 00:03:00,160 and here, my _ID field. 38 00:03:00,430 --> 00:03:07,570 So I know that in authors, in books I have objectId stored where the matching equivalence can be found 39 00:03:07,600 --> 00:03:11,680 in _ID in the authors collection. 40 00:03:11,810 --> 00:03:20,340 Last but not least, you give this an alias under which this will be merged and I'll name this creators, the 41 00:03:20,340 --> 00:03:21,290 name is up to you here. 42 00:03:22,530 --> 00:03:26,940 If you hit enter, let's pretty print this, 43 00:03:27,130 --> 00:03:33,500 we get back this result. What we see here is we get our book, actually all the books we have in there, 44 00:03:33,560 --> 00:03:37,390 only one in our case with the name with the authors and the objectIDs 45 00:03:37,400 --> 00:03:39,590 but then a new key was added, 46 00:03:39,680 --> 00:03:40,640 creators 47 00:03:40,820 --> 00:03:47,030 and that actually holds the author data which lived in a different collection and which was merged 48 00:03:47,060 --> 00:03:49,520 into a result by the aggregate 49 00:03:49,520 --> 00:03:51,660 and the lookup function. 50 00:03:51,710 --> 00:03:57,440 So this is very useful for merging data in one step and it allows you to get the best of both worlds, 51 00:03:57,710 --> 00:04:01,250 having it split up and still fetching it in one go. 52 00:04:01,250 --> 00:04:07,730 Now this still is not an excuse for always using references because obviously this costs more performance 53 00:04:07,850 --> 00:04:09,670 than having an embedded document, 54 00:04:09,680 --> 00:04:13,520 so if you can and if your application needs it, go for an embedded document. 55 00:04:13,610 --> 00:04:19,760 If you have to use references or if you want to use references, well then this lookup step in the aggregate 56 00:04:19,760 --> 00:04:23,450 method can at least help you get the data you need. 57 00:04:23,450 --> 00:04:28,490 Now it was just a first look at aggregate, as I mentioned we'll have a whole module where we see what 58 00:04:28,490 --> 00:04:30,340 else aggregate can do for us. 59 00:04:30,350 --> 00:04:30,890 Be aware of 60 00:04:30,890 --> 00:04:33,660 lookup for merging your relations though.