Complete Intro to Databases

# MongoDB Logical Operators

## Brian Holt

SQLite Cloud

### Check out a free preview of the full Complete Intro to Databases course

The "MongoDB Logical Operators" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian uses logical operators to add conditions to the data queried, and limits the amount of data queried by using a projection. A projection select only the queried data, and not the entire document.

Preview
Close

### Transcript from the "MongoDB Logical Operators" Lesson

[00:00:00]
>> Let's talk about logical operators. So we just talked about query operators with like non equals and equals and greater than and greater than or equal to. So we're gonna be talking now about, let's say you needed two things to be true at the same time. So let's say you wanted to combine two logical operators and say like, I want to find pets between the ages of four and eight so it would be both of those statements to be true, the way they would do that is with a logical operator.

[00:00:32]
We gonna say db dot pets dot find Al right. [INAUDIBLE] I swear this is my day job. Say count, want to say type, Bird, and then we're going to say dollar sign and to get multiple different conditions evaluating the true here. This is an array that we're going to pass to it.

[00:00:55]
I'm just going to close all these off so I can get it right. And then we're going to say age is another object which is greater than or equal to four. And then we're going to do another object here where age is another object of less than or equal to eight.

[00:01:22]
There you go. Next thing here you can notice that my shell is smart enough to highlight the bracket or square bracket or whatever I'm on. So now this should work. And now we can see that we have 556 birds that are between the ages of four and eight.

[00:01:49]
So all and does is an array of things that have to be true. So in this case, all of these things have to evaluate to true. You also have, or so, or it can be one of these can be equal to true. You have nor so that none of them can be equal to true.

[00:02:11]
Then you also have knot that will evaluate to all those to not being true. Cool. Does anyone have any questions about that? Cool. So you can see that using JavaScript objects, it's a little awkward and some of the ways you have to do it but you can end up with some pretty expressive queries.

[00:02:40]
It just ends up with a lot of nesting of objects. Okay? There's some more operators we're not going to talk about them now. Like there's one you can query like what type? Like what data type is something. So if you have name and you wanna query that of all names that are of type number just to find things that are wrong in your your collection.

[00:03:16]
That's possible. Another thing that MongoDB is now capable of doing is geospatial query. So let's say you have a bunch of points on a map and you wanna find the closest point to another point that's in your collection. You can do geospatial queries. Again, we're not going to do that today one because I've never had to, to be honest with you, but it is possible.

[00:03:37]
But if you have like find a pet adoption agency near you, and you have all of their longitudes and latitudes, you can do a geospatial query to figure out which one's the closest to based on a point. Another one's going to be important that we're going to do right now is sorts.

[00:03:56]
So let's find the Let's find the oldest dogs in our collection. So we're gonna say db dot pets dot find and we'll find like the top ten or something like that. And we're gonna find things of type dog. And then we're gonna say, sort like that and give it a sorting object.

[00:04:23]
So we are gonna sort by age, and you can either sort by ascending which would be one, or descending which would be negative one. Okay, and then we'll say limit five. And you can see there, we have five dogs here that are all 17. So that's what that dot sort does there for you that allows you to sort by ascending or descending, you can also sort by two things at once.

[00:04:55]
So these ones are all named dog, which is kind of interesting. Let's say we wanted to sort by breed as a secondary sort. [NOISE] Breed one. So now you can see here, that's sorting by breed ascending right. So African gray goes first. I don't really have an African trade dog but that sounds fun.

[00:05:22]
Or again, you can do here negative one. One I can see there is tabbies last. So you can have multiple faceted sorts as well.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses