1 00:00:00,180 --> 00:00:01,410 ‫So I've mentioned this before, 2 00:00:01,410 --> 00:00:03,420 ‫but let's do a little zoom in 3 00:00:03,420 --> 00:00:06,360 ‫on the conditional rights of DynamoDB. 4 00:00:06,360 --> 00:00:08,520 ‫So this is for the write operations. 5 00:00:08,520 --> 00:00:10,904 ‫So we have PutItem, UpdateItem, 6 00:00:10,904 --> 00:00:13,347 ‫DeleteItem, and BatchWriteItem. 7 00:00:13,347 --> 00:00:16,280 ‫And the idea is that you can specify a Condition expression 8 00:00:16,280 --> 00:00:20,490 ‫that's going to determine which items should be modified. 9 00:00:20,490 --> 00:00:22,200 ‫Now you have several conditions possible. 10 00:00:22,200 --> 00:00:25,230 ‫We'll have a look at many of these in examples. 11 00:00:25,230 --> 00:00:28,736 ‫So we have attributes_exists or attributes_not_exists, 12 00:00:28,736 --> 00:00:30,240 ‫attribute_type, so this is to check whether 13 00:00:30,240 --> 00:00:32,400 ‫or not an attribute_exists doesn't exist 14 00:00:32,400 --> 00:00:34,590 ‫and is of the correct type, 15 00:00:34,590 --> 00:00:38,160 ‫contains and begins_with, this is for string comparison. 16 00:00:38,160 --> 00:00:41,520 ‫And then we have the IN keyword to check different values. 17 00:00:41,520 --> 00:00:42,840 ‫For example, for product category 18 00:00:42,840 --> 00:00:44,700 ‫to be in two different categories. 19 00:00:44,700 --> 00:00:48,330 ‫And Price we have between :low and :high. 20 00:00:48,330 --> 00:00:49,590 ‫But we can also, for example, 21 00:00:49,590 --> 00:00:53,010 ‫do price over or price less than specific values. 22 00:00:53,010 --> 00:00:54,840 ‫And finally if we want to check for string length, 23 00:00:54,840 --> 00:00:56,850 ‫we have the size attributes. 24 00:00:56,850 --> 00:00:59,340 ‫Okay, so you have seen Filter Expressions 25 00:00:59,340 --> 00:01:01,290 ‫and you've seen Condition Expressions. 26 00:01:01,290 --> 00:01:04,950 ‫So Filter Expressions filter the results of a read query. 27 00:01:04,950 --> 00:01:08,070 ‫So Filter Expressions are related to read queries, 28 00:01:08,070 --> 00:01:12,630 ‫whereas Condition Expressions are only for rights operations 29 00:01:12,630 --> 00:01:17,630 ‫and they tell DynamoDB which rights should succeed or not. 30 00:01:18,510 --> 00:01:21,240 ‫So let's do an example, for example, an Update Item. 31 00:01:21,240 --> 00:01:24,870 ‫So here we update an item on the table product catalog 32 00:01:24,870 --> 00:01:27,720 ‫and we say that we want to set the price to be equal 33 00:01:27,720 --> 00:01:30,090 ‫to the current price minus discounts 34 00:01:30,090 --> 00:01:33,570 ‫only if the price is over to a specific limit. 35 00:01:33,570 --> 00:01:35,820 ‫That's our condition expression. 36 00:01:35,820 --> 00:01:38,910 ‫Now to fill in what is the discount and what is the limit? 37 00:01:38,910 --> 00:01:42,030 ‫We pass in the file://values.json 38 00:01:42,030 --> 00:01:43,620 ‫which has the discount to be a number 39 00:01:43,620 --> 00:01:48,620 ‫of 150 and the limit to be a number of 500. 40 00:01:48,660 --> 00:01:51,450 ‫So that means that if we have this item 41 00:01:51,450 --> 00:01:54,930 ‫in our DynamoDB table with a key 4, 5, 6 42 00:01:54,930 --> 00:01:58,200 ‫if we apply this Update Item command is going to 43 00:01:58,200 --> 00:02:02,130 ‫be transformed to be now having a price of 500. 44 00:02:02,130 --> 00:02:05,340 ‫That's because indeed before the price was strictly 45 00:02:05,340 --> 00:02:09,480 ‫over the limit of 500 because 650 is greater than 500. 46 00:02:09,480 --> 00:02:13,230 ‫But if we apply yet again the same command now 47 00:02:13,230 --> 00:02:14,400 ‫it's not going to succeed. 48 00:02:14,400 --> 00:02:17,511 ‫The price will never go below 500 because, well, 49 00:02:17,511 --> 00:02:21,390 ‫the condition expression would evaluate to being false. 50 00:02:21,390 --> 00:02:24,083 ‫So as you can see, condition expressions vary 51 00:02:24,083 --> 00:02:29,083 ‫whether or not the item has the value over 500 or not 52 00:02:29,790 --> 00:02:31,230 ‫and they will determine whether or not 53 00:02:31,230 --> 00:02:33,450 ‫the item will be updated. 54 00:02:33,450 --> 00:02:34,560 ‫So if we have Delete Item 55 00:02:34,560 --> 00:02:37,980 ‫if we can check for example, the attribute_not_exists. 56 00:02:37,980 --> 00:02:39,090 ‫So this only succeeds 57 00:02:39,090 --> 00:02:42,480 ‫if the attribute doesn't exist yet when there's no value. 58 00:02:42,480 --> 00:02:44,663 ‫For example, we're saying hey, delete an item 59 00:02:44,663 --> 00:02:48,810 ‫or delete many items if you have a batch right item 60 00:02:48,810 --> 00:02:51,921 ‫we can do delete one item for our product catalog 61 00:02:51,921 --> 00:02:55,200 ‫if the attribute price doesn't exist. 62 00:02:55,200 --> 00:02:56,033 ‫That makes sense. 63 00:02:56,033 --> 00:02:59,370 ‫If we have an item in our catalog but we don't have a price 64 00:02:59,370 --> 00:03:00,990 ‫we may not be able to sell it. 65 00:03:00,990 --> 00:03:03,723 ‫So we may want to clean up our table and delete it. 66 00:03:04,650 --> 00:03:07,350 ‫Alternatively, you have attribute_exists. 67 00:03:07,350 --> 00:03:10,530 ‫So it is the opposite of attributes not exist. 68 00:03:10,530 --> 00:03:12,810 ‫And here we can check if something exists. 69 00:03:12,810 --> 00:03:13,643 ‫For example, 70 00:03:13,643 --> 00:03:17,190 ‫if we have ProductReviews.Onestar that exists 71 00:03:17,190 --> 00:03:18,780 ‫that means that our product review 72 00:03:18,780 --> 00:03:20,640 ‫our product actually has one star and 73 00:03:20,640 --> 00:03:24,060 ‫so we can delete it altogether from our DynamoDB table 74 00:03:24,060 --> 00:03:26,550 ‫but we don't have it then don't do anything. 75 00:03:26,550 --> 00:03:28,890 ‫So you can start seeing the usefulness of it. 76 00:03:28,890 --> 00:03:30,000 ‫So there's a good use case 77 00:03:30,000 --> 00:03:31,980 ‫for attribute_not_exists and attribute_exists. 78 00:03:31,980 --> 00:03:34,590 ‫So if we have attribute_not_exists 79 00:03:34,590 --> 00:03:37,110 ‫and then we specify the partition key 80 00:03:37,110 --> 00:03:41,160 ‫what's going to happen is that if the item is already 81 00:03:41,160 --> 00:03:45,000 ‫in the database, then this condition will be evaluated false 82 00:03:45,000 --> 00:03:47,280 ‫and then we don't overwrite the item. 83 00:03:47,280 --> 00:03:49,380 ‫But if the item doesn't exist yet 84 00:03:49,380 --> 00:03:50,790 ‫then we can write the item. 85 00:03:50,790 --> 00:03:52,350 ‫So it's one way to make sure 86 00:03:52,350 --> 00:03:54,420 ‫that we never overwrite existing data 87 00:03:54,420 --> 00:03:56,700 ‫for this we do attribute_not_exists 88 00:03:56,700 --> 00:03:58,770 ‫and then we specify the partition key. 89 00:03:58,770 --> 00:04:02,160 ‫And in case we have the partition key and the sort key 90 00:04:02,160 --> 00:04:05,340 ‫and we want to make sure to again never override something 91 00:04:05,340 --> 00:04:07,710 ‫then you specify attributes_not_exists 92 00:04:07,710 --> 00:04:11,580 ‫partition key and attributes_not_exists sort key 93 00:04:11,580 --> 00:04:15,660 ‫and you're good to go. For other things, 94 00:04:15,660 --> 00:04:18,240 ‫for example, to check on values, you can have a look 95 00:04:18,240 --> 00:04:21,360 ‫at whether a product category is in different categories 96 00:04:21,360 --> 00:04:23,610 ‫or if the price is between a low and a high price. 97 00:04:23,610 --> 00:04:24,870 ‫And again, we can pass 98 00:04:24,870 --> 00:04:28,890 ‫in this values.json file we're specifying category 1 99 00:04:28,890 --> 00:04:30,600 ‫and category 2 as well 100 00:04:30,600 --> 00:04:33,870 ‫as the low of 500 and the high of 600. 101 00:04:33,870 --> 00:04:36,026 ‫And so when we apply Delete Item 102 00:04:36,026 --> 00:04:39,712 ‫in case the item does belong to this range 103 00:04:39,712 --> 00:04:41,160 ‫then we're good to go. 104 00:04:41,160 --> 00:04:42,572 ‫But as we can see, for example, 105 00:04:42,572 --> 00:04:47,520 ‫even though the products category Sporting Good is compliant 106 00:04:47,520 --> 00:04:48,630 ‫with our condition, 107 00:04:48,630 --> 00:04:50,820 ‫which is the first part of our condition, 108 00:04:50,820 --> 00:04:54,810 ‫the price is 650, which is not between 500 and 600. 109 00:04:54,810 --> 00:04:57,120 ‫And so the whole condition is false 110 00:04:57,120 --> 00:05:00,990 ‫and the item will not be deleted in this case. 111 00:05:00,990 --> 00:05:02,790 ‫You can also have a look at String Comparisons. 112 00:05:02,790 --> 00:05:05,160 ‫So you have begins_with and contains. 113 00:05:05,160 --> 00:05:07,170 ‫So here for example, we're saying, hey 114 00:05:07,170 --> 00:05:09,690 ‫I want to make sure to delete items 115 00:05:09,690 --> 00:05:14,250 ‫if it begins with http.:// 116 00:05:14,250 --> 00:05:17,790 ‫So we won't, don't want to keep any insecure images 117 00:05:17,790 --> 00:05:19,470 ‫in our product catalog. 118 00:05:19,470 --> 00:05:21,510 ‫So that's just some examples of course. 119 00:05:21,510 --> 00:05:23,400 ‫But what I want to remember out of it is that 120 00:05:23,400 --> 00:05:25,230 ‫Condition Expressions help you 121 00:05:25,230 --> 00:05:28,440 ‫create conditions that will tell that DynamoDB 122 00:05:28,440 --> 00:05:32,130 ‫whether or not to apply your right operations. 123 00:05:32,130 --> 00:05:33,270 ‫Alright, let's see for this lecture 124 00:05:33,270 --> 00:05:36,423 ‫I hope you liked it, and I will see you in the next lecture.