1 00:00:02,460 --> 00:00:11,310 Expression is useful if you want to compare two fields inside of one document and then find all documents 2 00:00:11,310 --> 00:00:15,450 where this comparison returns a certain result. Too abstract? 3 00:00:15,450 --> 00:00:16,860 Let's see what it does. 4 00:00:17,280 --> 00:00:26,420 Let's say we have a financial data database and there, I'll quickly prepare a collection, sales and 5 00:00:26,420 --> 00:00:33,060 I'll insert many sales, whoops, with square brackets. 6 00:00:33,070 --> 00:00:40,870 Now the first item has let's say a volume of 100 and a target of 120, 7 00:00:41,200 --> 00:00:48,490 the next item has a volume of 89 and a target of 80 8 00:00:48,670 --> 00:01:01,430 and I have another item where the volume is 200 and a target of 177. So now I inserted three entries here, 9 00:01:01,840 --> 00:01:08,040 we can quickly check them out, check them out with find and then pretty printing, 10 00:01:08,260 --> 00:01:10,130 so these are the three entries. 11 00:01:10,150 --> 00:01:17,440 Now let's say we want to find all entries, all items in this collection where the volume is above the 12 00:01:17,440 --> 00:01:18,120 target, 13 00:01:18,310 --> 00:01:21,170 this is a typical use case where expression can help us, 14 00:01:21,250 --> 00:01:23,370 now how do we use that? 15 00:01:23,590 --> 00:01:24,890 We use it as such, 16 00:01:24,940 --> 00:01:33,290 we access our sales collection and now in there, I specify my filter document and I first of all 17 00:01:33,290 --> 00:01:40,950 use expr which is this expression operator, now expr takes a document describing the expression. 18 00:01:41,300 --> 00:01:45,890 You can then use a couple of comparison operators like gt, 19 00:01:45,930 --> 00:01:52,760 now by the way, check out the official docs to learn more about valid expressions and which operators 20 00:01:52,770 --> 00:01:55,750 you can use there. So gt is one, 21 00:01:55,750 --> 00:01:57,250 now here I pass an array 22 00:01:57,340 --> 00:02:00,480 and it will basically compare all these fields. 23 00:02:00,850 --> 00:02:06,550 Now the interesting thing here is I won't enter values manually to find out which one is the greater 24 00:02:06,730 --> 00:02:08,580 item because that's pretty redundant, 25 00:02:08,800 --> 00:02:14,040 instead here, we now reference names of fields in our data. 26 00:02:14,190 --> 00:02:18,470 Now in our data, remember we have volume and target as fields, 27 00:02:18,550 --> 00:02:22,750 now you don't enter volume and target like this, 28 00:02:22,800 --> 00:02:24,540 this would be invalid 29 00:02:24,550 --> 00:02:26,980 instead you wrap them in quotation marks, 30 00:02:26,980 --> 00:02:32,350 you wrap the data in quotation marks, like this, volume and target, 31 00:02:32,830 --> 00:02:41,070 so now it would compare volume and target and only find documents where volume is greater than target. 32 00:02:41,110 --> 00:02:46,790 However, this would still not work because right now it would still treat this as a hard input from your 33 00:02:46,790 --> 00:02:47,190 site, 34 00:02:47,240 --> 00:02:52,760 so basically as a string and it would compare if volume is greater than target and it would return everything 35 00:02:52,820 --> 00:02:56,260 because from an alphabetic perspective, this is true 36 00:02:57,310 --> 00:03:02,170 and that's probably also nice to know that greater than does work here too. 37 00:03:02,590 --> 00:03:07,790 If we want to refer to the field names, we have to add a dollar sign at the beginning, 38 00:03:07,810 --> 00:03:15,670 this tells mongodb hey please look at the volume field and use the value of that in this expression 39 00:03:16,180 --> 00:03:19,320 and you can't use that in every place in your queries, 40 00:03:19,330 --> 00:03:24,820 it does work on the expression query though and later in the aggregation framework module, you'll find 41 00:03:24,820 --> 00:03:27,550 more places where you can use that. 42 00:03:27,550 --> 00:03:29,340 So now we're referring to the field names 43 00:03:29,350 --> 00:03:36,060 and if I now hit enter, I only find two documents where the volume indeed is higher than the target. 44 00:03:36,190 --> 00:03:42,310 The first document where that was not the case is not included in the results subset and that is of 45 00:03:42,310 --> 00:03:50,740 course super useful because with this dynamic approach, we have an easy time of fetching the data where 46 00:03:50,800 --> 00:03:53,770 some condition within the document is met, 47 00:03:53,800 --> 00:04:00,260 so where two kind of related fields have a certain kind of relation that we check here. 48 00:04:00,270 --> 00:04:04,560 Now there also is a more complex form of expression, 49 00:04:04,930 --> 00:04:12,610 let's say we don't just want to compare whether volume is greater than target but let's say if the volume 50 00:04:12,730 --> 00:04:24,670 is above 190, the difference to target has to be at least 10. So we should still find this document, 51 00:04:24,770 --> 00:04:26,220 the difference is only nine here 52 00:04:26,240 --> 00:04:29,150 but it's also not greater than 190, 53 00:04:29,270 --> 00:04:35,330 for this however, we should also still find it because it will be greater than 190 and the difference 54 00:04:35,330 --> 00:04:37,820 will then be greater than 10 to target 55 00:04:37,910 --> 00:04:40,890 but how will we write this? 56 00:04:41,330 --> 00:04:48,950 For this, you have to adjust the part inside your greater than or whatever your condition here is. The 57 00:04:49,040 --> 00:04:55,430 first value will now be a document and there, you use a special dollar sign cond operator which is related 58 00:04:55,430 --> 00:04:56,350 to expression, 59 00:04:56,360 --> 00:05:01,340 so this cond is related to this expression so both can work in tandem. 60 00:05:01,460 --> 00:05:06,750 The condition allows you to specify document that describes a conditional value which it will derive 61 00:05:06,740 --> 00:05:07,300 here, 62 00:05:07,370 --> 00:05:13,280 so remember, we're still looking for a value we can use as a first element in our greater than array and 63 00:05:13,280 --> 00:05:20,090 we just want to calculate that value dynamically. For that, in that cond document, we first of all specify 64 00:05:20,210 --> 00:05:21,500 a condition with if 65 00:05:21,710 --> 00:05:27,330 and that has to be named if, if is then another comparison operator. 66 00:05:27,740 --> 00:05:32,040 So here we can check for let's say greater than or equal, 67 00:05:32,210 --> 00:05:44,670 again we have two values in here and we want to check whether volume is greater than or equal to 190. 68 00:05:44,670 --> 00:05:51,430 Now if this is met, then make sure to place the comma correctly after a closing square bracket and curly 69 00:05:51,430 --> 00:05:55,460 brace, then we have a certain value we want to use, 70 00:05:55,480 --> 00:06:03,100 so this indeed is named then and we can either hardcode a value here or we calculate it dynamically. 71 00:06:03,100 --> 00:06:12,020 Now here we can calculate dynamically by passing another document and you subtract, which is an operator 72 00:06:12,020 --> 00:06:15,960 you can use to subtract two values, 73 00:06:16,370 --> 00:06:21,760 you specify an array and you could hardcode values to subtract these two values from each other 74 00:06:22,220 --> 00:06:28,880 and by the way subtract and so on, these are all aggregation pipeline operators, so no worries if that 75 00:06:28,880 --> 00:06:29,300 is a lot 76 00:06:29,300 --> 00:06:36,840 right now, in the aggregation framework module, this will all become a bit clearer and also easier. So 77 00:06:36,850 --> 00:06:43,150 subtract will subtract the second element we specified in this array from the first element. 78 00:06:43,150 --> 00:06:49,930 Now what I want to do here is essentially I want to subtract 10 from volume, so that the volume I use 79 00:06:49,930 --> 00:06:52,790 for comparison is reduced by 10. 80 00:06:52,810 --> 00:07:00,450 So here I will use volume as a first element and then I subtract 10 hardcoded in there, 81 00:07:00,810 --> 00:07:04,510 ok so I subtract 10 from the value that is stored in the volume 82 00:07:04,510 --> 00:07:10,150 for all the items where the volume is greater than 190. Now that's one case, 83 00:07:10,150 --> 00:07:15,670 now let's add an else case because what is the case for items where the volume is not greater than 84 00:07:15,670 --> 00:07:17,440 or equal to 190? 85 00:07:17,680 --> 00:07:25,460 Well there, I'll simply just use the volume without any changes, like this. 86 00:07:26,420 --> 00:07:33,010 So now we finished our condition and that is the first value in our array here, 87 00:07:33,170 --> 00:07:34,070 so the first value is 88 00:07:34,070 --> 00:07:38,840 now simply either the volume or if the volume is greater than or equal to 190, 89 00:07:38,930 --> 00:07:40,850 it's the volume minus 10, 90 00:07:41,180 --> 00:07:48,910 so for our volume of 200, we'll compare one of 190. Now the value we compare it to 91 00:07:49,020 --> 00:07:56,250 and now I'm in the array related to the greater than operator, the value I will compare it to still is target 92 00:07:56,250 --> 00:07:58,880 here. 93 00:07:58,890 --> 00:08:07,680 Now if I hit enter, I do find the same set of data as before but this will change if I switch the 10 which 94 00:08:07,710 --> 00:08:15,990 I subtract from my volume to let's say 20, that I require a difference between volume, to 30, that I 95 00:08:15,990 --> 00:08:22,740 require a difference of volume to target of 30 if the volume is 190 or higher. 96 00:08:22,740 --> 00:08:28,590 Now I only get back this element with a volume of 89 because it's not as high, 97 00:08:28,590 --> 00:08:33,210 it's not making it into this comparison here 98 00:08:33,660 --> 00:08:37,890 and therefore I take the volume unchanged to compare it to the target value. 99 00:08:38,310 --> 00:08:42,060 But for this document, the volume is 200 100 00:08:42,270 --> 00:08:48,780 hence it makes it into this condition and into this then block, therefore the volume I take for comparison 101 00:08:48,780 --> 00:08:50,810 is reduced by 30 102 00:08:51,030 --> 00:08:58,580 and that is then not greater than the target of 177 anymore because 200 minus 30 is of course 107 103 00:08:58,590 --> 00:08:59,560 . 104 00:08:59,950 --> 00:09:02,830 Now this is a quite complex case here 105 00:09:03,060 --> 00:09:09,100 and this is also not a query you might be running all the time but it can be very helpful. 106 00:09:09,210 --> 00:09:16,290 Now for all the comparison and change operators which we used in there, again this is something you'll 107 00:09:16,290 --> 00:09:22,290 also see in the aggregation framework module and it is something where you can dive into the official docs 108 00:09:22,290 --> 00:09:24,550 to see all possible values there. 109 00:09:24,550 --> 00:09:29,760 This is a quite powerful command and therefore definitely worth playing around with 110 00:09:29,790 --> 00:09:34,940 but for now, I just wanted to explain how it generally works and how you have to think about it.