How to Use the Not Operator in MongoDB
The Not operator can be extremely useful when working with a database query. If you’re not familiar with how the Not operator works we’ll explain it in the first section of this article. We hope you continue reading as we explain what the Not operator is and how you can apply it to make some of your database queries more succinct. It is a common concept not only in databases but in programming in general and is a good concept to have a firm handle on. We hope you keep reading as we introduce you to the Not Operator.
What is the Not Operator?
$not is MongoDB’s implementation of the NOT logical operator that exists in most database technologies. You can use it with any other query expression such as less than ($lt), greater than ($gt), or equal to ($eq). In short it returns anything but the documents that match the inner condition. It’s very similar to the logical Not Operator in programming which is typically seen as
!. In programming the Not Operator negates the expression it operates on.
A More Relatable Example
Let’s use a non-database example in case you think better in those terms. Let’s say you hate a specific brand of orange juice called “JJ’s Oranges”. If someone were going to the grocery store and asked you if you wanted some orange juice, you might say something like “Yes, I want anything but JJ’s Oranges”. This is similar to what the Not Operator does. You could list ALL the brands of Orange Juice that would be suitable, but that would be a very long request. Instead it would be easier for you to say that you want anything but that specific brand.
Just to give you a little preview, a query like this would look something like this:
How to Use It
The Not Operator is what’s referred to as a meta-conditional because it can only be used after another criteria you specify.
The syntax for using the
$not operator is as follows:
Let’s take a look at an example then dissect it afterwards:
This would read as to find all the records in inventory who have a price not greater than $1.99. This is similar to saying that you’d like the records whose price is less than or equal to $1.99. There is one good question to ask yourself is “What happens if the price field doesn’t exist?”. The Not operator would return those records where the price doesn’t exist because if there is no price, it is NOT greater than $1.99.
A popular use case would be if there are some exceptions in your data and you want to perform some operation on the entire data set, minus those exceptions.
This will return all the products whose barcode don’t match that number. ( Again including products that don’t have a barcode. )
You can apply this Not operator on top of any conditional.
We hope that this explanation of the Not Operator will help you in writing more succinct queries. This may seem like an operator that you wouldn’t use that often but it is often easier to specify the set you DON’T want and then using the
$not operator on top of it. Please don’t hesitate to reach out to us at Object Rocket if you need help managing your MongoDB.
Pilot the ObjectRocket platform free for 30 Days
It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!PILOT FREE FOR 30 DAYS