‫Querying for data in a database is one
‫of the most important operations that we have in databases.
‫And so, let's now take a look at a couple of query operators
‫in MongoDB, starting with some simple ones
‫and then moving on to some really complex queries.
‫And, to start, let's give ourselves some space here.
‫And so, you already know, the easiest way
‫to basically query for all the documents
‫in a certain collection is to just use find
‫without passing anything in there.
‫So, that's what we've been doing until this point.
‫And so, it gives us simply this result
‫with all the documents
‫that are in a certain collection basically
‫without any searching criteria.
‫But, now, let's say that we actually only want one tour
‫and we already know its name.
‫And so, we can search for that tour
‫using the name that we know.
‫And so, we us db.tours
‫and, again, find.
‫But, this time, we're gonna pass in a filter object.
‫So, again, we need an object here.
‫And so, you start to see now that in MongoDB,
‫really everything works with objects.
‫And, this will be even more prominent a bit later
‫in this lecture.
‫So, inside this object, we pass in the filter.
‫So, basically the search criteria
‫that we want to search for.
‫So, we simply set the name to the tour name
‫that we want to search for.
‫So, let's say The Forest Hiker.
‫And, that's actually it.
‫So, that is our search criteria or the search filter.
‫And, if we had returned now,
‫then we only get this one tour
‫where the name matches exactly the one that we passed in.
‫And, we could do, of course, the same for anything else.
‫For example, for the difficulty,
‫we could search for tours
‫that have an easy in difficulty.
‫So, difficulty and easy.
‫Close the object, close the function.
‫And so, we get all the tours where the difficulty is easy.
‫Right now, there is only one tour,
‫but if we had multiple tours with difficulty set
‫to easy, then, of course,
‫it would return all of these documents.
‫So, that is the easiest way
‫that we can search for documents.
‫Now, let's take it to the next level
‫by using some special query operators.
‫And, what I want to do is to search for tours
‫which have a price below 500.
‫And, the way it works is like this.
‫So, we always use db. The collection name .find.
‫And then, again, or filter object.
‫And so, remember, I want to search for prices,
‫so I say price.
‫And now, I want all the tours with a price below 500.
‫So, how do I do that?
‫Well, I need to use the less than operator
‫and it works like this.
‫We need to define yet a new object
‫where we set the lte property to 500,
‫then close that object,
‫and then close that first price object
‫and then close the function.
‫So, that looks very weird, I know,
‫but this is how we use query operators in MongoDB.
‫So, again, lte stands for less than
‫because that is what we're searching for.
‫Where the price is less than 500.
‫And, this special sign here is reserved in MongoDB
‫for its operators.
‫So, whenever you see this dollar sign here in MongoDB,
‫you know that it's a Mongo operator.
‫So, the weirdest part here is probably
‫that we have to do it inside a new object,
‫but if you think about it, it's actually really the only way
‫to specify that the price should not simply be 500,
‫but something else.
‫So, we have to set the price to something else than 500.
‫And, the best way really is
‫to just use another object in there.
‫So, that's exactly how MongoDB then works.
‫So, let's try this out now, hit return.
‫And, indeed, we get our two documents
‫where the price property is below 500.
‫Great, next up, let's actually search
‫for two search criteria at the same time.
‫So, what I want to do next is to search for documents
‫which have the price less or equal than 500,
‫which is what we had here already,
‫but also, at the same time, the rating greater
‫or equal to 4.8.
‫And so, that should then give us only this tour here.
‫So, only this one
‫because this is the only one which has the price below 500
‫and the rating equal or greater to 4.8.
‫So, it's 4.8 and so that one is the one
‫that should match our query.
‫So, let's clear it to take away some of the confusion.
‫Now, I want this result here
‫so that we can compare it with the next result.
‫And so, let's now write out the query that I just described.
‫So, find and, again, this one here is
‫gonna be just the same.
‫So, we want the price to be less or equal than 500.
‫Or, we could actually simply say less than 500.
‫So, lte is less than or equal and lt is simply less than.
‫So, this is the first part, the price less than 500.
‫And, at the same time, we want the rating
‫to be greater or equal to 4.8.
‫And, that's easy.
‫All we have to do is to specify a second field
‫in our filter object.
‫So, first, the price and now the rating.
‫So, we want the rating to be
‫and you can probably guess it,
‫greater than or equal 4.8.
‫Now, close this filter object and close the function.
‫So, let's test it out.
‫And, indeed, it gives us the result that we were expecting.
‫So, the only document where both the search criteria here
‫are true at the same time.
‫So, just to recap, when we want to search
‫for two criteria at the same time,
‫which basically is an and query,
‫so price less than 500, and rating greater
‫or equal to 4.8.
‫Well, the only thing that we have to do is
‫to specify two fields in the filter object.
‫And, I know this looks quite confusing.
‫I felt exactly the same when I was learning this stuff,
‫but I hope that you can still follow me
‫so that we can now actually take it even one level further.
‫So, here, we did an and query.
‫So, querying for documents where these two conditions
‫are both true, but now let's do an or query.
‫So, basically searching for all the documents
‫where either this part here is true
‫or this part is true.
‫So, db.tours.find
‫and let's close it here already
‫because this is gonna be an even more confusing one.
‫So, we want to do an or query and the way it works
‫with MongoDB operators is like this.
‫We say or and then here, we specify an array.
‫And, in this array, we will then put the two conditions
‫where we want one of them to be true.
‫So, again, I know this looks confusing,
‫but let's write it out now and I will then explain it again
‫when we're already doing that.
‫So, the first condition is the price should
‫be less than 500.
‫So, basically the same as before.
‫So, that is the first object.
‫So, all of this, so the first condition,
‫basically the first filter,
‫and then the second one.
‫So, the rating should be, just like before,
‫greater or equal than 4.8.
‫Close this one and close this one.
‫And so, that's actually already it.
‫So, again, to recap here, we start with the or operator
‫and the or operator accepts an array of conditions.
‫So, that's why we then create this array here
‫and this array will then contain one object
‫for each of our filters basically.
‫So, we want either this one to be true
‫or this one.
‫So, let's try to see what our results are gonna be.
‫And so, actually we get all of the three tours.
‫And so, let's analyze why that is.
‫So, the first one has a price less than 500.
‫And so, that's why it got included here.
‫It doesn't have the rating greater or equal to 4.8,
‫so it's just 4.7, but since we're doing an or query here,
‫only one of the conditions needs to be true.
‫And so, that's why this document here got selected
‫and is included in our output.
‫Next up, this one also has the price less or equal than 500
‫and also, at the same time, it has the rating great
‫or equal than 4.8.
‫And so, it got included actually
‫for both of them being true.
‫Then, the last one does not have the price less than 500,
‫but it does have the rating greater or equal to 4.8.
‫And so, again, one of the two conditions is true
‫and since we're doing an or query,
‫this tour also got included.
‫So, let's clear this and only one more,
‫which is gonna be kind of the same.
‫I'm simply gonna change from less than to greater than.
‫And, what I want you to do now is
‫to guess the output of this one based
‫on the previous result.
‫So, can you guess what the result will be?
‫So, let's take a look.
‫And so, now, indeed, we no longer have all three tours,
‫but only the second and the third one
‫because, remember, the first tour
‫had the price less than 500
‫and also the rating less than 4.8.
‫And so, none of the conditions were applying
‫and so it's no longer included.
‫Only the second and the third one,
‫which now has the price also not greater than 500,
‫but it has the rating of 4.8
‫and so it's still included.
‫And, the last one, of course, has everything true
‫and so, of course, it's also here.
‫Just one more thing that I wanted to show you here is
‫that besides our filter object, so this one,
‫we can also pass in an object for projection.
‫So, what projection means is that we simply want
‫to select some of the fields in the output.
‫So, let me show it to you and it's very simple.
‫All we have to do is, for example, say name equals to one.
‫So, what this means is that we only want the name
‫to be in the output and so that's why we set name to one.
‫All the others are not gonna appear in this case.
‫So, let me show that to you.
‫And, indeed, we only have the name
‫and no longer all these other properties.
‫And, this can be very useful in some cases.
‫The idea is, of course, also showing up,
‫but that's always there.
‫There's no way of removing that.
‫Okay, cool.
‫So, I think the main goal of this lecture is now achieved,
‫which was basically to get you familiar
‫with the way we query in MongoDB
‫and also show you these complex MongoDB operators
‫that we have here.
‫For some reason, I cannot select it,
‫but you know what I mean.
‫So, we have greater than,
‫we have or, and we really have a ton
‫of other operators still.
‫So, this is only the surface,
‫but throughout the rest of the course, you will, of course,
‫get to know a couple of other operators.
‫So, this is just to get you familiar,
‫get you started with querying in MongoDB.
‫And, I think in this lecture, we did that successfully.