WEBVTT

00:05.360 --> 00:07.070
Have room and welcome back.

00:07.120 --> 00:10.270
So in this video we're going to be talking about queering Dockum.

00:10.280 --> 00:14.230
So how can we query the documents that we inserted into the collections.

00:14.470 --> 00:16.170
So let's get started.

00:16.350 --> 00:21.550
So the query data from among mango bee collection you need to use the Mongo D-B find method.

00:21.560 --> 00:22.010
OK.

00:22.150 --> 00:29.530
So the find method is used to basically find as the name might suggest to find some documents inside

00:29.860 --> 00:33.130
the original collection that you put the documents in.

00:33.130 --> 00:40.790
So the basic syntax of the find function is to just do D-B dot collection underscore name so this is

00:40.870 --> 00:43.360
the collection name and then find.

00:43.490 --> 00:49.430
OK so your IDB for example dot my collection dot find.

00:49.480 --> 00:50.350
OK.

00:50.600 --> 00:54.310
Find so fully that that makes sense.

00:54.440 --> 00:59.560
And the find method will display all the documents in a non-structured way.

00:59.570 --> 01:00.460
So what does that mean.

01:00.470 --> 01:08.300
Well if you're right if we just do for example let's go back to my virtual machine and I have here my

01:08.300 --> 01:11.620
column has some a document inside.

01:11.660 --> 01:20.240
So if I say dvd my column does find and I just give it an empty document then you will find all the

01:20.240 --> 01:23.440
data and you'll notice that it looks very very jibberish.

01:23.450 --> 01:23.700
Right.

01:23.700 --> 01:26.090
It's very hard to read this.

01:26.090 --> 01:31.400
So since we don't want to you know you don't want to be debugging and trying to find where is the key

01:31.400 --> 01:36.290
that you're looking for and all that stuff than what we can do is to use the pretty function.

01:36.290 --> 01:38.340
So the Preedy function over here.

01:38.420 --> 01:44.550
So this pretty function over here makes whatever fine prints as the name might suggest printing.

01:44.630 --> 01:47.310
So it makes it look much much much more pretty.

01:47.330 --> 01:54.050
So you usually do D-B that might call up my collection that find and then once you have the results

01:54.050 --> 01:56.280
from find then you do not pretty.

01:56.360 --> 01:58.390
So you make it pretty.

01:58.430 --> 02:04.080
So if we go back here and you'll actually notice that I already did it before this Glasgow added to

02:04.270 --> 02:04.950
DBD.

02:04.970 --> 02:06.960
Michael I'm done fine.

02:07.220 --> 02:12.590
And then I'll give it an empty document and then not pretty.

02:12.740 --> 02:15.020
And then it will display the column just like that.

02:15.020 --> 02:20.570
So it shows you ID object title this description this and so on.

02:20.570 --> 02:25.850
So this is a very very much easier to debug than of course reading just having to read this and trying

02:25.850 --> 02:27.010
to debug.

02:27.020 --> 02:31.010
So that's just one point where many of us talking about.

02:31.010 --> 02:36.860
So apart from the find method there's also find one method and that returns only one document.

02:36.920 --> 02:43.080
So the form for the find function we're going to talk more about it the Find function we give it a door

02:43.330 --> 02:49.310
a document that this basically this document tells us what to what to search for.

02:49.310 --> 02:55.460
So let's say you have you want to search for all the customers whose names are let's say Jack.

02:55.580 --> 03:00.420
So you say name and then Jack.

03:00.570 --> 03:07.730
So maggoty would say for all the possible documents which have the name key with a jack value.

03:07.790 --> 03:08.350
OK.

03:08.540 --> 03:13.790
So but sometimes you might have more than one customer has yet you might have five or six jacks.

03:13.790 --> 03:14.280
Right.

03:14.420 --> 03:17.870
So in that case they're fine with return all of that.

03:18.030 --> 03:20.520
Whereas find one would only return one of them.

03:20.530 --> 03:25.820
So sometimes you don't just want one of them sees you find one or if you want to return all the set

03:25.820 --> 03:34.070
of possible documents that satisfy your search condition or your query condition then you would use

03:34.160 --> 03:36.350
just the normal find.

03:36.620 --> 03:42.160
Now for the query documents we have to pass through that to them document as I mentioned.

03:42.320 --> 03:43.660
And it's called a filter.

03:43.760 --> 03:51.800
So a filter is just a document and it should contain some information about what you expect their returned

03:51.950 --> 03:53.000
documents to have.

03:53.000 --> 03:56.580
So some conditions to satisfy that that are returned.

03:56.630 --> 04:02.570
So the query the document on the basis of some condition you can use the following operations so a bunch

04:02.570 --> 04:04.840
of operations that we'll talk about.

04:05.150 --> 04:06.700
So the first one is a quality.

04:06.710 --> 04:10.040
Let's say you want to return all the documents.

04:10.120 --> 04:14.180
Who's who's one of the keys matches one of the values.

04:14.210 --> 04:19.320
So you would do D-B for example that my column does find title.

04:19.340 --> 04:22.050
Mungo D-B overview not pretty.

04:22.100 --> 04:22.690
OK.

04:22.760 --> 04:31.010
So now you're specifying that you only want the records which have which which have which satisfy that

04:31.540 --> 04:37.270
the which satisfy that the title of them is man would be over.

04:37.370 --> 04:45.320
So if we go back here to our database or here and then I do D-B my collection that I find but then this

04:45.320 --> 04:47.980
time I pass it titled.

04:48.230 --> 04:55.410
And then two dots meaning I want the title to be mangled with the B over me.

04:55.610 --> 04:57.140
OK and I click enter.

04:57.350 --> 05:01.230
So that would show me that in the normal form but I want it to be pretty.

05:01.250 --> 05:04.380
So I do not pretty.

05:04.550 --> 05:09.860
And then it shows me here that this is the record that satisfies title longer D-B overview.

05:09.860 --> 05:16.580
Now if I chase this somehow right to be overviews just small letter you set up a capital letter then

05:16.580 --> 05:21.580
it would return nothing because none of the records in my column satisfy this search result.

05:21.590 --> 05:22.770
OK.

05:23.500 --> 05:31.010
There is also the less than OK so the less than is good as the name suggest you might want a key to

05:31.010 --> 05:34.040
have a value which is less than something right.

05:34.160 --> 05:42.350
So let's say you want to return all the records where the likes are less than 50 for example or less

05:42.350 --> 05:43.420
than something right.

05:43.580 --> 05:50.830
So you would do the meta find up do GBW my collection find and then the like.

05:51.510 --> 05:58.300
For the key and for the value you want to say I want this value to be less than 50 use this notation.

05:58.320 --> 05:59.750
So open parentheses.

05:59.810 --> 06:01.780
Open curly brackets.

06:01.830 --> 06:03.220
Dollar sign.

06:03.340 --> 06:10.910
LTE meaning less than and then 2.50 close curly brackets close curly brackets the outer one and then

06:10.900 --> 06:11.790
end up pretty.

06:11.790 --> 06:14.550
So let's try and let's try and do that.

06:14.580 --> 06:16.100
In our example.

06:16.170 --> 06:21.690
So if we go back here and then I want to find all the records with likes less than 50 so none of them

06:21.690 --> 06:25.270
right because there's only one record and it has 100 likes.

06:25.320 --> 06:26.160
But let's just try it.

06:26.160 --> 06:29.850
So if you do D-B dot my column that's fine.

06:30.390 --> 06:34.300
And then I want the record to have likes.

06:35.100 --> 06:41.110
And inside the lives I want I want it to have less than so I open a curly brackets.

06:41.160 --> 06:43.750
I do a dollar sign then LTE.

06:43.770 --> 06:44.890
So less then.

06:44.980 --> 06:51.440
And then two dots and then 50 Celeste click enter.

06:51.820 --> 06:53.070
And of course dot pretty.

06:53.070 --> 06:54.380
Of course.

06:55.240 --> 07:01.360
So you didn't get any results because of the only document in there has 100 likes which is more than

07:01.360 --> 07:02.120
50.

07:02.320 --> 07:09.430
But if if we instead replace the 50 was 200 for example then we would get again our record 1 on it because

07:09.430 --> 07:13.720
it satisfies the condition that the likes are less than 200 hundred hundreds.

07:13.750 --> 07:21.430
Less than two Okay now the third one the third operation that we can do is less than or equal do.

07:21.540 --> 07:24.020
So as you might have guessed it's key.

07:24.120 --> 07:25.590
And then LTE.

07:25.620 --> 07:27.070
So less than or equal.

07:27.120 --> 07:28.270
And then the value.

07:28.510 --> 07:33.060
So for example the need to find column that my column the find.

07:33.240 --> 07:38.730
So this one here only finds records which have less than strictly less than this one searches for less

07:38.730 --> 07:39.770
than or equal.

07:39.900 --> 07:40.570
OK.

07:40.890 --> 07:46.180
So give me my call got that find likes and less than or equal to 50 50.

07:46.230 --> 07:50.250
Let's go back and try to do this in our in our database.

07:50.450 --> 08:01.440
So if I go here and I do D-B my column find and then likes you if I say likes on and then I want it

08:01.440 --> 08:05.460
to be less than or equal to so dollar less than or equal to.

08:05.700 --> 08:14.420
And then the value is 50 and then duck for that I wouldn't have any records right.

08:14.460 --> 08:20.610
But if I changed this to 100 I want any record which has less than or equal to 100.

08:20.970 --> 08:23.210
Then I would get again this record over here.

08:23.220 --> 08:27.140
So this record has 100 which is less than or equal to 100.

08:27.150 --> 08:27.740
Right.

08:28.080 --> 08:28.590
OK.

08:28.740 --> 08:30.140
So let's move up.

08:31.930 --> 08:35.360
Now this the other ones are greater then.

08:35.380 --> 08:36.670
So it's key.

08:36.670 --> 08:45.610
Greater than GTY and then value and you would do it again divvied up my column find likes and 50 over

08:45.610 --> 08:46.120
here.

08:46.120 --> 08:47.990
So let's try to do that one too.

08:48.040 --> 08:54.550
So if you go back to our database and we do get beat up my call my collection does fine.

08:55.150 --> 09:03.340
And then for the likes I'm going to say likes and then for the value I want it to be Dollar greater

09:03.340 --> 09:05.910
than and 15.

09:06.120 --> 09:06.770
OK.

09:06.930 --> 09:08.780
And then pretty.

09:09.000 --> 09:13.580
So you get Again likes because this number of likes is more than 50.

09:13.650 --> 09:22.010
Now if I instead replace this 50 with 100 then I would get no records because this one is less than

09:22.010 --> 09:24.530
or equal to a hundred not greater than three.

09:24.860 --> 09:31.590
So it wouldn't be filtered now the first one is the greater than or equal.

09:31.690 --> 09:32.050
OK.

09:32.060 --> 09:37.160
So at least like there's less and less than or equal to there is greater then there is greater than

09:37.160 --> 09:39.410
or equal and it's GTY.

09:39.470 --> 09:42.410
OK so let's take an example again.

09:42.740 --> 09:48.560
Shouldn't be too different so we just we can just replace this year greater than or equal and that this

09:48.560 --> 09:53.180
time it will include this record over here because it's bigger than or equal to on.

09:53.240 --> 09:53.710
Right.

09:53.840 --> 09:58.420
So if it's bigger than or equal to a hundred then that's a valid.

09:58.770 --> 10:04.770
OK so little last filter that we can also pass or not the last one but one the last one.

10:04.770 --> 10:10.670
In these types of filters in the compares competitor filters are not equals.

10:10.680 --> 10:13.020
OK so they're not equals basically is.

10:13.040 --> 10:19.990
And so you're saying that I don't want to I want this key to be not equal to this value.

10:20.130 --> 10:26.850
So bring me all of that all the documents for example such that their name is not.

10:26.850 --> 10:28.360
For example Jack.

10:28.470 --> 10:29.170
OK.

10:29.580 --> 10:33.280
So I want all documents except the documents whose name is Jack.

10:33.400 --> 10:35.820
So you put here not equal to Jack.

10:35.820 --> 10:36.110
Right.

10:36.120 --> 10:37.230
And they are here.

10:37.400 --> 10:39.710
So let's take an example on right.

10:40.970 --> 10:48.590
On our database Frigo here and we choose not equal to so I don't want the likes to be equal to 100.

10:48.800 --> 10:54.380
So if I click enter then I wouldn't get any records right because none of the other records there's

10:54.380 --> 10:55.060
only one right.

10:55.130 --> 10:56.690
There's only one document right.

10:56.750 --> 11:00.850
None of the other documents have anything other than 100 likes.

11:01.070 --> 11:07.370
But if I put for example a hundred and one or a hundred and ten then there is one document which has

11:07.370 --> 11:11.090
likes not equal to 110 which is this one here.

11:11.510 --> 11:13.180
So hopefully this makes sense.

11:14.630 --> 11:15.000
OK.

11:15.000 --> 11:18.970
Another thing is that we can use and in in the in the filter.

11:19.080 --> 11:24.510
So you can for example you would if you if if your common with programming and you sometime you say

11:24.570 --> 11:32.620
if X is less than 10 and Y is bigger than 20 then do something right.

11:32.790 --> 11:38.130
So if I want to put this an operator here then I need to specify how I can do it.

11:38.150 --> 11:41.550
And the way we do it is we use the and operator.

11:41.550 --> 11:46.920
So you do dollar and and then we open up.

11:46.980 --> 11:48.450
And then two dots right.

11:48.600 --> 11:50.390
And then we open an array.

11:50.620 --> 11:54.140
And inside this array we have a tuple right.

11:54.240 --> 12:00.110
A bunch of documents that where I want the ends of all of these documents right.

12:00.120 --> 12:02.960
So I want this and this and this and and so on.

12:03.060 --> 12:03.540
OK.

12:03.660 --> 12:05.310
So for example here I can write.

12:05.310 --> 12:16.420
So this one my B likes and then let's see open parentheses.

12:16.440 --> 12:20.800
And then Dollar Sign greater than 50.

12:20.850 --> 12:23.630
So I want the likes to be greater than 50.

12:24.030 --> 12:35.230
And then here I can have but less than 100 right so I want the likes be Ltds less than and then a hundred

12:36.330 --> 12:43.560
so that way that means I'm searching for all the records where the likes are greater than 50 and the

12:43.560 --> 12:46.430
legs are less than.

12:46.480 --> 12:50.040
So LTE 100 less than 100 right.

12:50.070 --> 12:53.820
Sure remember here we have just in case you forget the letters there.

12:53.880 --> 12:57.720
LTE LTE G.T. ZTE and any.

12:57.840 --> 12:58.760
OK.

12:59.370 --> 13:04.710
So this search this would search for all of the posts where the likes are greater than 50 but less than

13:04.710 --> 13:05.320
a hundred.

13:05.370 --> 13:05.990
OK.

13:06.330 --> 13:09.380
So let's take an example of this in our database.

13:09.630 --> 13:17.670
So I want to search for all the posts with likes greater than 50 and less than or equal to 100.

13:17.700 --> 13:19.700
So this one would be included.

13:19.880 --> 13:20.540
OK.

13:20.550 --> 13:23.470
So I would do D-B got my collection.

13:23.550 --> 13:24.920
Not fine.

13:24.960 --> 13:25.600
Right.

13:25.860 --> 13:32.590
And then the first thing is I want to give it a document and inside this document I'm going to have

13:32.590 --> 13:42.160
a dollar and then two dots and then the the array signs on I'm sending get many conditions.

13:42.360 --> 13:45.470
So I have two conditions in this case right.

13:45.480 --> 13:51.010
The first condition is I want the number of likes to be.

13:51.210 --> 14:02.430
So to me to be greater than 50 and the second condition is I want the number of likes to be greater

14:02.430 --> 14:09.280
than a dollar and then less than or equal to and then 100.

14:09.280 --> 14:15.660
And then once I'm done with that then I prettify everything and I get this post because the number of

14:15.660 --> 14:17.760
likes is greater than 100.

14:17.780 --> 14:19.900
But less than or equal to 1.

14:20.220 --> 14:22.560
So this is satisfied pretty nice.

14:23.040 --> 14:24.110
So this is how you do it.

14:24.120 --> 14:26.320
And in Mungiki.

14:26.580 --> 14:27.910
Now if you want to do.

14:27.960 --> 14:29.150
Or statement.

14:29.190 --> 14:29.760
OK.

14:29.820 --> 14:36.140
So if you want to do an OR statement then we can also use a dollar or it's just like we use the dollar

14:36.160 --> 14:41.860
end so to query documents based on the condition you need to use the or keyword.

14:41.940 --> 14:46.810
Following is the basics syntax of or so this is an example it doing or.

14:46.830 --> 14:50.730
And then two dots and then open an array and inside of it.

14:50.850 --> 15:01.290
But many conditions so this is exactly like saying if x is equal to one or Y is equal to 1 rest of this

15:01.290 --> 15:03.250
is equal equal to 1.

15:03.420 --> 15:05.410
Then do something right.

15:05.970 --> 15:12.850
So this is saying if this is true in a document or if this is true in the document then add this document

15:12.850 --> 15:14.630
to the list that you will return to me.

15:14.640 --> 15:15.020
Right.

15:15.120 --> 15:19.870
So I want all the documents which satisfied one of these two conditions over here.

15:20.070 --> 15:20.660
OK.

15:20.970 --> 15:27.450
So let's take an example of this and this will be the final example that we take in this video so we

15:27.450 --> 15:28.720
will do DBD.

15:28.740 --> 15:31.140
My collection does fine.

15:31.320 --> 15:33.090
And so I want.

15:33.090 --> 15:40.000
So let's open of the document and that I want and or conditions so or.

15:40.380 --> 15:43.400
And then two dots and then an array.

15:43.740 --> 15:52.140
And inside this array I have two conditions that either one of them I want I want I like to be equal

15:52.140 --> 16:06.820
to I say 30 which is not true or I want the title to be Mungo D.B overview.

16:07.070 --> 16:07.990
OK.

16:08.260 --> 16:10.490
So I want the likes to be.

16:10.530 --> 16:12.950
Or the title to be Mangu with me over you.

16:13.120 --> 16:18.970
So I've tried prettify and we think that I should also get this record again because even though the

16:18.970 --> 16:26.500
lights are not 30 but the title is actually Mongo D-B overview so it counts in the records that satisfy

16:26.500 --> 16:28.180
either this one or this one.

16:28.180 --> 16:30.610
So it returns to me.

16:30.760 --> 16:33.550
So hopefully all of this makes sense.

16:33.820 --> 16:39.550
It just needs a little bit of practice to practice different and remember some of the keywords and also

16:39.550 --> 16:43.040
don't forget that sometimes people confuse the curly brackets.

16:43.210 --> 16:49.910
So these curly brackets are often various So there's always an outer curly bracket right.

16:49.930 --> 16:50.460
These ones.

16:50.460 --> 16:58.330
This one and this one after find and then after you open that or with that array there's a curly brackets

16:58.330 --> 17:05.710
inside and then sometimes for this value area here sometimes you have curly brackets and then Orphy

17:06.280 --> 17:07.960
or a greater than for example.

17:07.960 --> 17:13.060
So this is another curly bracket right so you just have to make sure you're you're inputting your curly

17:13.060 --> 17:16.000
brackets correctly so you don't miss anything.

17:16.570 --> 17:19.020
So yeah this will be it for this video.

17:19.120 --> 17:20.810
And until the next video.

17:20.920 --> 17:21.660
Happy go.
