﻿1
00:00:01,090 --> 00:00:03,300
‫As the next feature in our API,

2
00:00:03,300 --> 00:00:04,910
‫we have field limiting,

3
00:00:04,910 --> 00:00:07,290
‫so, basically, in order to allow clients

4
00:00:07,290 --> 00:00:08,900
‫to choose which fields they want

5
00:00:08,900 --> 00:00:10,543
‫to get back in the response.

6
00:00:11,960 --> 00:00:14,530
‫So, for a client, it's always ideal

7
00:00:14,530 --> 00:00:16,980
‫to receive as little data as possible,

8
00:00:16,980 --> 00:00:19,170
‫in order to reduce the bandwidth

9
00:00:19,170 --> 00:00:21,890
‫that is consumed with each request.

10
00:00:21,890 --> 00:00:23,890
‫And that's, of course, especially true

11
00:00:23,890 --> 00:00:27,650
‫when we have really data-heavy data sets, right?

12
00:00:27,650 --> 00:00:29,470
‫And so it's a very nice feature

13
00:00:29,470 --> 00:00:33,793
‫to allow the API user to only request some of the fields.

14
00:00:36,470 --> 00:00:41,323
‫So, as the third feature, we will have field limiting.

15
00:00:42,460 --> 00:00:45,280
‫And, just like before, let me start by showing you

16
00:00:45,280 --> 00:00:47,823
‫how it's gonna work here in Postman.

17
00:00:49,130 --> 00:00:53,083
‫So, we're gonna specify a field called fields,

18
00:00:53,083 --> 00:00:54,710
‫and then the name of the fields

19
00:00:54,710 --> 00:00:56,810
‫that we actually want to receive.

20
00:00:56,810 --> 00:00:58,853
‫So let's say we only want the name,

21
00:01:00,720 --> 00:01:01,773
‫the duration,

22
00:01:03,570 --> 00:01:08,570
‫the difficulty, and the price, all right.

23
00:01:08,600 --> 00:01:11,580
‫And so, the implementation will actually be very similar

24
00:01:11,580 --> 00:01:13,513
‫to what we did before with sorting.

25
00:01:14,860 --> 00:01:19,417
‫So, if req.query.fields,

26
00:01:22,400 --> 00:01:24,270
‫then let's actually, again, create a variable

27
00:01:24,270 --> 00:01:28,310
‫for these fields here because just like before,

28
00:01:28,310 --> 00:01:30,610
‫Mongodbs, actually requests a string

29
00:01:30,610 --> 00:01:34,100
‫with the field name separated by spaces, all right?

30
00:01:34,100 --> 00:01:36,100
‫Now let me actually start by writing out

31
00:01:36,100 --> 00:01:40,883
‫that part, okay, so, the query itself.

32
00:01:41,770 --> 00:01:45,887
‫So query is equal to query.select, okay?

33
00:01:48,710 --> 00:01:51,740
‫And here, for example, it expects a string like,

34
00:01:51,740 --> 00:01:56,740
‫name then space, duration and a price for example, okay.

35
00:01:59,420 --> 00:02:01,540
‫And so this way, it will only select

36
00:02:01,540 --> 00:02:04,070
‫these three field names and send back

37
00:02:04,070 --> 00:02:07,020
‫the result only containing that, okay?

38
00:02:07,020 --> 00:02:09,150
‫And actually, this operation of selecting

39
00:02:09,150 --> 00:02:14,150
‫only certain field names is called projecting okay?

40
00:02:14,720 --> 00:02:17,110
‫So, let's now go ahead and create basically,

41
00:02:17,110 --> 00:02:21,930
‫the string, just like we did up there okay.

42
00:02:21,930 --> 00:02:26,930
‫So we start with req.query.fields,

43
00:02:28,047 --> 00:02:29,663
‫then we split it by a comma,

44
00:02:30,840 --> 00:02:32,490
‫and join it with an empty string.

45
00:02:33,800 --> 00:02:36,570
‫With a space actually, sorry.

46
00:02:36,570 --> 00:02:38,590
‫So with the space and so that's, then,

47
00:02:38,590 --> 00:02:42,683
‫gonna produce this exact string with this exact format.

48
00:02:44,160 --> 00:02:47,033
‫Then here, all we have to do is to use these fields.

49
00:02:49,490 --> 00:02:53,150
‫Then, just like before, I also want to add a default

50
00:02:53,150 --> 00:02:57,280
‫so in case the user does not specify the fields field,

51
00:02:57,280 --> 00:03:01,240
‫okay, so, in that case, we will just

52
00:03:03,230 --> 00:03:08,230
‫actually remove something, okay?

53
00:03:08,540 --> 00:03:12,870
‫And let me show that to you first, so down here,

54
00:03:12,870 --> 00:03:16,880
‫we always have this underscore underscore V,

55
00:03:16,880 --> 00:03:21,000
‫which is set to zero, and Mongodbs just creates these fields

56
00:03:21,000 --> 00:03:23,020
‫because it uses them internally.

57
00:03:23,020 --> 00:03:25,850
‫And we could disable them, but that's not a good practice.

58
00:03:25,850 --> 00:03:29,094
‫Again, because Mongodbs actually uses them, alright,

59
00:03:29,094 --> 00:03:31,530
‫but what we can do is to basically

60
00:03:31,530 --> 00:03:34,725
‫never send them to the client, so we can exclude them.

61
00:03:34,725 --> 00:03:38,470
‫And the way we do that is to, instead of doing

62
00:03:38,470 --> 00:03:41,290
‫underscore underscore V like before,

63
00:03:41,290 --> 00:03:43,320
‫so like we did up here, basically,

64
00:03:43,320 --> 00:03:46,490
‫we just prefix it with a minus, okay?

65
00:03:46,490 --> 00:03:49,860
‫And minus is then not including, but excluding.

66
00:03:49,860 --> 00:03:51,690
‫So what we had before here again,

67
00:03:51,690 --> 00:03:53,900
‫with the name and duration and price,

68
00:03:53,900 --> 00:03:56,890
‫that was to include these fields in the response.

69
00:03:56,890 --> 00:03:58,580
‫But this way, with the minus,

70
00:03:58,580 --> 00:04:02,560
‫we have everything except the V field here okay?

71
00:04:02,560 --> 00:04:05,903
‫So we're excluding only this field, alright?

72
00:04:07,060 --> 00:04:08,660
‫So let's go ahead and test that.

73
00:04:09,870 --> 00:04:14,870
‫Send it, and let's wait for it and yeah, indeed so,

74
00:04:15,430 --> 00:04:18,250
‫the ID is by default, always including okay?

75
00:04:18,250 --> 00:04:21,140
‫So we cannot remove that, but besides that,

76
00:04:21,140 --> 00:04:24,700
‫we have just the name, duration, difficulty, and price,

77
00:04:24,700 --> 00:04:28,360
‫okay, and we could, of course, also use that minus

78
00:04:28,360 --> 00:04:31,070
‫so that exclusion here as well.

79
00:04:31,070 --> 00:04:33,570
‫So we could have everything, minus the name

80
00:04:33,570 --> 00:04:35,653
‫and minus the duration, for example.

81
00:04:38,280 --> 00:04:40,200
‫So let's test that as well.

82
00:04:40,200 --> 00:04:44,160
‫And so, if you now check it, you will not see the name,

83
00:04:44,160 --> 00:04:46,853
‫and not see the duration anywhere here.

84
00:04:48,635 --> 00:04:50,690
‫Right, and now just as a final test,

85
00:04:50,690 --> 00:04:53,670
‫let's see if our default works as well.

86
00:04:53,670 --> 00:04:56,930
‫Sending the response and so, now we no longer have

87
00:04:56,930 --> 00:05:01,410
‫that V variable here set to zero in all the documents.

88
00:05:01,410 --> 00:05:04,840
‫All right, great, now there's one last thing

89
00:05:04,840 --> 00:05:06,520
‫that I want to show you which is that

90
00:05:06,520 --> 00:05:09,950
‫we can also exclude fields right from the schema.

91
00:05:09,950 --> 00:05:11,680
‫Alright, and that can be very useful,

92
00:05:11,680 --> 00:05:13,573
‫for example, when we have sensitive data

93
00:05:13,573 --> 00:05:15,720
‫that should only be used internally.

94
00:05:15,720 --> 00:05:17,261
‫For example, stuff like passwords

95
00:05:17,261 --> 00:05:19,750
‫should never be exposed to the client

96
00:05:19,750 --> 00:05:21,320
‫and so therefore, we can exclude

97
00:05:21,320 --> 00:05:23,372
‫some fields right in the schema.

98
00:05:23,372 --> 00:05:26,320
‫So for example, we might not want the user

99
00:05:26,320 --> 00:05:29,780
‫to see when exactly each tour was created.

100
00:05:29,780 --> 00:05:31,950
‫For example, tour might already be

101
00:05:31,950 --> 00:05:34,020
‫kind of old or something and so,

102
00:05:34,020 --> 00:05:38,320
‫let's say we want to always hide this createdAt field,

103
00:05:38,320 --> 00:05:40,753
‫alright, so we can go into our schema,

104
00:05:42,050 --> 00:05:44,800
‫which is in the tour model of course,

105
00:05:44,800 --> 00:05:47,950
‫and then at createdAt, we simply set

106
00:05:47,950 --> 00:05:52,023
‫the select property here to false.

107
00:05:54,050 --> 00:05:58,090
‫And now when we try to get our results,

108
00:05:58,090 --> 00:06:00,490
‫you see that it's actually no longer there okay?

109
00:06:02,090 --> 00:06:03,460
‫And so like this, we can basically,

110
00:06:03,460 --> 00:06:06,690
‫permanently hide this from the output.

111
00:06:06,690 --> 00:06:11,110
‫Perfect, so one more feature that is actually working now

112
00:06:11,110 --> 00:06:13,723
‫and so let's now move on right to the next one.

