1 00:00:02,320 --> 00:00:04,770 Now let's do a different query, 2 00:00:04,840 --> 00:00:12,650 we still have that text index on the description and let's search for awesome T-shirt. If we do this, 3 00:00:12,650 --> 00:00:16,580 I find both entries because I have awesome here too in the first product. 4 00:00:16,670 --> 00:00:22,510 Now finding both might be fine but you typically would want to order this differently, 5 00:00:22,550 --> 00:00:27,980 for us humans it's pretty clear that the second product is the better hit because there we have both awesome 6 00:00:28,070 --> 00:00:34,160 and t-shirt so would be nice if that would come first in our result list and we can actually get there 7 00:00:34,820 --> 00:00:40,400 because mongodb does something interesting or special when managing such a text index or when searching 8 00:00:40,400 --> 00:00:47,900 for text in a text index, we can find out how it scores its results and let's first of all find out with 9 00:00:47,900 --> 00:00:49,120 the help of projection. 10 00:00:49,220 --> 00:00:56,420 So here I'll add a second argument to my find method to project the results and I will simply output 11 00:00:56,510 --> 00:00:58,110 a score field here 12 00:00:59,430 --> 00:01:08,220 where I can use a special meta operator to add text score. This is a meta field added or managed by mongodb 13 00:01:08,270 --> 00:01:09,920 for text searches 14 00:01:09,930 --> 00:01:11,090 here with the $text 15 00:01:11,090 --> 00:01:14,470 operator on a text index. 16 00:01:14,470 --> 00:01:16,240 If I do this, you see the score. 17 00:01:16,410 --> 00:01:18,340 mongodb assigned to a result 18 00:01:18,580 --> 00:01:22,550 and here, it even automatically sort it by that score now as you can tell. 19 00:01:22,840 --> 00:01:30,680 So here we see that the score is much higher for T-shirt and awesome than for just awesome and it automatically 20 00:01:30,700 --> 00:01:31,620 sort it for us here 21 00:01:31,630 --> 00:01:39,430 but if we want to make sure, we can of course also add sort here and we can then sort by score where 22 00:01:39,430 --> 00:01:48,020 score is a document with $meta and then textScore written like this as a text and I should add 23 00:01:48,020 --> 00:01:51,640 a dot in front of pretty. And now, it's definitely sorted, 24 00:01:51,650 --> 00:01:58,220 it was before but now we enforce sorting by looking at that meta text score which mongodb manages 25 00:01:58,220 --> 00:01:58,980 for us. 26 00:01:59,210 --> 00:02:03,630 So this is simply a construct you can remember for when working with text and text indexes, 27 00:02:03,800 --> 00:02:10,400 you can extract this meta information text score and both these words have to be written like this from 28 00:02:10,490 --> 00:02:15,760 mongodb so to say, it manages that for us and we can see how it scores the results 29 00:02:15,770 --> 00:02:18,890 and as you can see, we can then use that to order too.