Have room and welcome back.
So in this video we're going to be talking about queering Dockum.
So how can we query the documents that we inserted into the collections.
So let's get started.
So the query data from among mango bee collection you need to use the Mongo D-B find method.
OK.
So the find method is used to basically find as the name might suggest to find some documents inside
the original collection that you put the documents in.
So the basic syntax of the find function is to just do D-B dot collection underscore name so this is
the collection name and then find.
OK so your IDB for example dot my collection dot find.
OK.
Find so fully that that makes sense.
And the find method will display all the documents in a non-structured way.
So what does that mean.
Well if you're right if we just do for example let's go back to my virtual machine and I have here my
column has some a document inside.
So if I say dvd my column does find and I just give it an empty document then you will find all the
data and you'll notice that it looks very very jibberish.
Right.
It's very hard to read this.
So since we don't want to you know you don't want to be debugging and trying to find where is the key
that you're looking for and all that stuff than what we can do is to use the pretty function.
So the Preedy function over here.
So this pretty function over here makes whatever fine prints as the name might suggest printing.
So it makes it look much much much more pretty.
So you usually do D-B that might call up my collection that find and then once you have the results
from find then you do not pretty.
So you make it pretty.
So if we go back here and you'll actually notice that I already did it before this Glasgow added to
DBD.
Michael I'm done fine.
And then I'll give it an empty document and then not pretty.
And then it will display the column just like that.
So it shows you ID object title this description this and so on.
So this is a very very much easier to debug than of course reading just having to read this and trying
to debug.
So that's just one point where many of us talking about.
So apart from the find method there's also find one method and that returns only one document.
So the form for the find function we're going to talk more about it the Find function we give it a door
a document that this basically this document tells us what to what to search for.
So let's say you have you want to search for all the customers whose names are let's say Jack.
So you say name and then Jack.
So maggoty would say for all the possible documents which have the name key with a jack value.
OK.
So but sometimes you might have more than one customer has yet you might have five or six jacks.
Right.
So in that case they're fine with return all of that.
Whereas find one would only return one of them.
So sometimes you don't just want one of them sees you find one or if you want to return all the set
of possible documents that satisfy your search condition or your query condition then you would use
just the normal find.
Now for the query documents we have to pass through that to them document as I mentioned.
And it's called a filter.
So a filter is just a document and it should contain some information about what you expect their returned
documents to have.
So some conditions to satisfy that that are returned.
So the query the document on the basis of some condition you can use the following operations so a bunch
of operations that we'll talk about.
So the first one is a quality.
Let's say you want to return all the documents.
Who's who's one of the keys matches one of the values.
So you would do D-B for example that my column does find title.
Mungo D-B overview not pretty.
OK.
So now you're specifying that you only want the records which have which which have which satisfy that
the which satisfy that the title of them is man would be over.
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
time I pass it titled.
And then two dots meaning I want the title to be mangled with the B over me.
OK and I click enter.
So that would show me that in the normal form but I want it to be pretty.
So I do not pretty.
And then it shows me here that this is the record that satisfies title longer D-B overview.
Now if I chase this somehow right to be overviews just small letter you set up a capital letter then
it would return nothing because none of the records in my column satisfy this search result.
OK.
There is also the less than OK so the less than is good as the name suggest you might want a key to
have a value which is less than something right.
So let's say you want to return all the records where the likes are less than 50 for example or less
than something right.
So you would do the meta find up do GBW my collection find and then the like.
For the key and for the value you want to say I want this value to be less than 50 use this notation.
So open parentheses.
Open curly brackets.
Dollar sign.
LTE meaning less than and then 2.50 close curly brackets close curly brackets the outer one and then
end up pretty.
So let's try and let's try and do that.
In our example.
So if we go back here and then I want to find all the records with likes less than 50 so none of them
right because there's only one record and it has 100 likes.
But let's just try it.
So if you do D-B dot my column that's fine.
And then I want the record to have likes.
And inside the lives I want I want it to have less than so I open a curly brackets.
I do a dollar sign then LTE.
So less then.
And then two dots and then 50 Celeste click enter.
And of course dot pretty.
Of course.
So you didn't get any results because of the only document in there has 100 likes which is more than
50.
But if if we instead replace the 50 was 200 for example then we would get again our record 1 on it because
it satisfies the condition that the likes are less than 200 hundred hundreds.
Less than two Okay now the third one the third operation that we can do is less than or equal do.
So as you might have guessed it's key.
And then LTE.
So less than or equal.
And then the value.
So for example the need to find column that my column the find.
So this one here only finds records which have less than strictly less than this one searches for less
than or equal.
OK.
So give me my call got that find likes and less than or equal to 50 50.
Let's go back and try to do this in our in our database.
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
to be less than or equal to so dollar less than or equal to.
And then the value is 50 and then duck for that I wouldn't have any records right.
But if I changed this to 100 I want any record which has less than or equal to 100.
Then I would get again this record over here.
So this record has 100 which is less than or equal to 100.
Right.
OK.
So let's move up.
Now this the other ones are greater then.
So it's key.
Greater than GTY and then value and you would do it again divvied up my column find likes and 50 over
here.
So let's try to do that one too.
So if you go back to our database and we do get beat up my call my collection does fine.
And then for the likes I'm going to say likes and then for the value I want it to be Dollar greater
than and 15.
OK.
And then pretty.
So you get Again likes because this number of likes is more than 50.
Now if I instead replace this 50 with 100 then I would get no records because this one is less than
or equal to a hundred not greater than three.
So it wouldn't be filtered now the first one is the greater than or equal.
OK.
So at least like there's less and less than or equal to there is greater then there is greater than
or equal and it's GTY.
OK so let's take an example again.
Shouldn't be too different so we just we can just replace this year greater than or equal and that this
time it will include this record over here because it's bigger than or equal to on.
Right.
So if it's bigger than or equal to a hundred then that's a valid.
OK so little last filter that we can also pass or not the last one but one the last one.
In these types of filters in the compares competitor filters are not equals.
OK so they're not equals basically is.
And so you're saying that I don't want to I want this key to be not equal to this value.
So bring me all of that all the documents for example such that their name is not.
For example Jack.
OK.
So I want all documents except the documents whose name is Jack.
So you put here not equal to Jack.
Right.
And they are here.
So let's take an example on right.
On our database Frigo here and we choose not equal to so I don't want the likes to be equal to 100.
So if I click enter then I wouldn't get any records right because none of the other records there's
only one right.
There's only one document right.
None of the other documents have anything other than 100 likes.
But if I put for example a hundred and one or a hundred and ten then there is one document which has
likes not equal to 110 which is this one here.
So hopefully this makes sense.
OK.
Another thing is that we can use and in in the in the filter.
So you can for example you would if you if if your common with programming and you sometime you say
if X is less than 10 and Y is bigger than 20 then do something right.
So if I want to put this an operator here then I need to specify how I can do it.
And the way we do it is we use the and operator.
So you do dollar and and then we open up.
And then two dots right.
And then we open an array.
And inside this array we have a tuple right.
A bunch of documents that where I want the ends of all of these documents right.
So I want this and this and this and and so on.
OK.
So for example here I can write.
So this one my B likes and then let's see open parentheses.
And then Dollar Sign greater than 50.
So I want the likes to be greater than 50.
And then here I can have but less than 100 right so I want the likes be Ltds less than and then a hundred
so that way that means I'm searching for all the records where the likes are greater than 50 and the
legs are less than.
So LTE 100 less than 100 right.
Sure remember here we have just in case you forget the letters there.
LTE LTE G.T. ZTE and any.
OK.
So this search this would search for all of the posts where the likes are greater than 50 but less than
a hundred.
OK.
So let's take an example of this in our database.
So I want to search for all the posts with likes greater than 50 and less than or equal to 100.
So this one would be included.
OK.
So I would do D-B got my collection.
Not fine.
Right.
And then the first thing is I want to give it a document and inside this document I'm going to have
a dollar and then two dots and then the the array signs on I'm sending get many conditions.
So I have two conditions in this case right.
The first condition is I want the number of likes to be.
So to me to be greater than 50 and the second condition is I want the number of likes to be greater
than a dollar and then less than or equal to and then 100.
And then once I'm done with that then I prettify everything and I get this post because the number of
likes is greater than 100.
But less than or equal to 1.
So this is satisfied pretty nice.
So this is how you do it.
And in Mungiki.
Now if you want to do.
Or statement.
OK.
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
end so to query documents based on the condition you need to use the or keyword.
Following is the basics syntax of or so this is an example it doing or.
And then two dots and then open an array and inside of it.
But many conditions so this is exactly like saying if x is equal to one or Y is equal to 1 rest of this
is equal equal to 1.
Then do something right.
So this is saying if this is true in a document or if this is true in the document then add this document
to the list that you will return to me.
Right.
So I want all the documents which satisfied one of these two conditions over here.
OK.
So let's take an example of this and this will be the final example that we take in this video so we
will do DBD.
My collection does fine.
And so I want.
So let's open of the document and that I want and or conditions so or.
And then two dots and then an array.
And inside this array I have two conditions that either one of them I want I want I like to be equal
to I say 30 which is not true or I want the title to be Mungo D.B overview.
OK.
So I want the likes to be.
Or the title to be Mangu with me over you.
So I've tried prettify and we think that I should also get this record again because even though the
lights are not 30 but the title is actually Mongo D-B overview so it counts in the records that satisfy
either this one or this one.
So it returns to me.
So hopefully all of this makes sense.
It just needs a little bit of practice to practice different and remember some of the keywords and also
don't forget that sometimes people confuse the curly brackets.
So these curly brackets are often various So there's always an outer curly bracket right.
These ones.
This one and this one after find and then after you open that or with that array there's a curly brackets
inside and then sometimes for this value area here sometimes you have curly brackets and then Orphy
or a greater than for example.
So this is another curly bracket right so you just have to make sure you're you're inputting your curly
brackets correctly so you don't miss anything.
So yeah this will be it for this video.
And until the next video.
Happy go.