This post is pre-publication. Feel free to leave a comment below or send feedback directly.

How to Use The MongoDB $OR Operator - JS Office Hour

How to Use The MongoDB $OR Operator - JS Office Hour

JS Office Hour is a weekly webinar where our instructors take on questions from Viking students and other coding communities. If you have a question you would like them to answer, tweet it to us on #JSOfficeHour.

Yep. We have a question from Reddit user with a bunch of dashes here, very creative little user name here. What we've got here is question about MongoDB, specifically about MongoDB operators. They would like to know what the "or" operator is used for. I used the word operator, because it's what this kind of thing is referred to not only in MongoDB, but in other Querty languages for SQL for example, and programing levels like Java Script. It is a logical operator. So if we take a look at some of the other operators that are available in MoboDB, there's quite a few, and they all follow this syntax of having dollar sign prefix. I'm not going to go through what all of these operators do, but what I will do is I'll give an example of how to use the "or" operator and what it means and what it's doing. If you take a look the counterpart to "or," just to explain things briefly, is "and". And if you're familiar with Java Script Logical operators, we have something like "false and true." Which would evaluate false, let's say something like "this with not run". 'Cause when you have "and" it's going to basically look at both of these and say, "Are these both true?" And they're not so this would not run. But with "or" ... We're going to say, "Is one of these true?" So this will run. And that's exactly the same case with "or" when you use it in your MongoDB Querts. So what we're going to do is we're going to look in our student's collection here, and we're going to find students that either have a subject of signs, or they have a score greater than or equal to 90 (is another operator that MongoDB uses). So this is essentially no different ... Actually, let me just put it in the comment. This is essentially no different than saying, "greater or equal to 90." And then we're going to say, "Find students who could also potentially have a first name that begins with the character S, using the regex there. So it's just no different than something like string.match.regexJavaScript. So I can actually copy and paste this into my MongoDB terminal, and we get back some results. I limited it to three results. If I take the limit away we'd see even more results. But the basic idea here is that all of these results are going to match one, at least one, of these three quick parameters. So they either have a subject of science, a score of greater than 90, or greater than or equal to 90, or they have a first name that begins with an S. So let's just take a look at a few. So this one doesn't fit the first two but it has a score of 90 or greater. This one not 90 or greater, not a subject of science, but first name begins with an S. And so on and so forth. If you analyze each one of these you'll find that it's at least one of these parameters. So yeah, I highly recommend that if you're getting MongoDB you go through and check out more of these Query operators. The MongoDB documentations fairly extensive. Just play around with it. Create your own collections and try out new Querts, and operators. I hope that helped you. Patrick: Yeah, so just a follow-up question, I see we have the representative, the Viking dog here with us, it's pretty cute. Are you saying that the MongoDB "or" operator basically operates just like the Boolean operator, like you would use an "if" statement, kind of works about the same way? Chris: Absolutely. If you could imagine instead here we had some kind of "if" statement, in here there is ... Somehow we're using the "or" here. And for each one of these we're iterating, it'd be something like we have a collection of students in here, we're going through for each and we have Query results. What would basically be happening is like, if this "or" returns true, then we would say "Query results push result." And then this is kind of like a sudo-code of what would be going on under the hood. In MongoDB. And it's effectively no different than what happens with this code here.

https://youtu.be/d5DAP_7rpIM

From n00b to ninja, we'll help you to become a developer
Subscribe to get expert guidance on learning, building, and getting hired delivered right to your inbox each week.


We guarantee your privacy 100%. Your information will not be shared.