MongoDB with Async Python Transcripts
Chapter: Document Databases
Lecture: Query Operators
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
If what we pass for these queries are prototypical JSON objects like the ISBN equals this or the rating equals five or something along those lines, that's cool when you're doing an equals, but database queries often involve greater than, less than, not equal to, and these types of things, right?
0:22
I would like all the users who have registered in this time window from yesterday to today. That's not an equal, right? So how do we do that?
0:30
Well, there are these operators for querying that allow you to express things like greater than and less than and so on.
0:37
So we can have, they all start with dollar, the special operators. We have dollar eq, that's just equal, that's kind of what we've been doing.
0:44
So usually don't need that, but there are places where you can use it. The dollar gt is greater than or gte is greater than or equal to.
0:54
We have less than, less than or equal to, not equal to. One that I find really valuable is in.
1:01
You might say, ""I've got a set of 10 objects, and I want to know if I want all the records where any of the values might match that.
1:10
I could say, ""Give me where the rating is in some number of rating values I want, like four, five, and nine. I don't know.
1:19
Maybe you want the extremes. You want zero and nine. Our little bookstore example is not exactly lending a bunch of great examples to come
1:28
to mind here. But that's a really, really common query that I do all the time.
1:35
There's more of these as well and you can see examples at just follow the URL here at the bottom.
1:40
There's a reference for all the native operators, at least for querying here. So you can go check them out.
1:46
This is one of the things that is not obvious when you get started with MongoDB because
1:50
you can't just use the greater than sign or less than, like you might in SQL. But there's not very
1:57
many of them. This is actually most of them that you get to use. So you can check them out and use
2:01
them appropriately. Again, we're not going to do this in our application. This is just when we're
2:07
playing with the shell and maybe exploring the data a little bit. And then we'll go back to
2:11
writing Python where you actually use like the greater than sign. Let's look at one real quick
2:16
example, suppose we want to find all the books that have ratings of nine or 10, not all of
2:23
the ratings, but they have at least one rating in that ratings list that's embedded in them
2:29
that has either nine or 10. And so we would write this we just say book dot find ratings
2:34
dot value to navigate that hierarchy. And then instead of nine, we say, pass in this
2:40
this little query object, curly brace, greater than or equal to colon 9. And that'll operate just as if we had said, you know, the greater than or
2:50
equal to sign, and 9 in a traditional query syntax like SQL.