Comprehensive Guide to Using the $ne Operator in MongoDB

In this tutorial, I am going to walk you through using the $ne operator in MongoDB.

MongoDB offers a variety of operators that can be used for a variety of purposes. It has provided developers with easy-to-understand as well as flexible operators that allow them to do a lot more than CRUD in a MongoDB-backed application. This is also one reason why MongoDB is so popular.

With the help of operators in MongoDB, you customize querying, printing, updating, and more. It has found fandom even among beginners along with senior developers. While the list of operators is huge, I am here to talk about the $ne operator in MongoDB also known as ‘not equal’.

The $ne operator allows us to look up and select documents where the value of the field is not equal to the given value. Note that this will also include those documents that do not contain that field.

In this guide, I will demonstrate an example so you can understand how you can use the $ne operator in MongoDB for many use cases and examples. This operator is also known as a not equal operator or an inequality operator.

So, let us get started.

Finding Documents with the $ne Operator in MongoDB

Let us assume we run a farm produce store application. And we also have a database for this purpose. For this guide, I have added created and added a few documents to my farms collection.

So let us get started. Follow the below steps to find documents using the $ne operator in MongoDB using the find() method.

  • Start your MongoDB database server
  • Pass this command to list all your databases and use your preferred one:
show dbs
use droneStore
  • Let us first take a look at all the farms that are registered on our application. We will then use the $ne operator or the not equal operator.
> db.farms.find({})

{ "_id" : ObjectId("6054f5397c7f6b1274b80939"), "products" : [ ObjectId("6054f5587c7f6b1274b8093a"), ObjectId("6055e998b1016a256057d538") ], "name" : "Lazy Sloth Holdings", "city" : "Nashville, Indiana", "email" : "[email protected]", "__v" : 2 }

{ "_id" : ObjectId("6055ac0b6a275d2394ced121"), "products" : [ ], "name" : "Gospel Flat Farms", "city" : "Bolinas", "email" : "[email protected]", "__v" : 0 }

{ "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"), "products" : [ ObjectId("615763b1b579ab008019c4c3") ], "name" : "William's Ranch", "city" : "Nashville, Indiana", "email" : "[email protected]", "__v" : 1 }

{ "_id" : ObjectId("605a31baa3e92a0f34774c88"), "products" : [ ObjectId("6157638db579ab008019c4c1") ], "name" : "Star Route Farms", "city" : "Pennysylvania", "email" : "[email protected]", "__v" : 1 }

{ "_id" : ObjectId("61576604ee49ac2db8adc6f0"), "products" : [ ], "name" : "Scotman's Greenlands", "city" : "Stratford-upon-Avon", "email" : "[email protected]", "__v" : 0 }

{ "_id" : ObjectId("61576681ee49ac2db8adc6f1"), "products" : [ ], "name" : "Huckleberry Acres", "city" : "Warwickshire, England", "email" : "[email protected]", "__v" : 0 }
  • Now, let us say we want to see all farms in our database whose city is not Nashville, Indiana. To do this, we will use the $ne operator now.
> db.farms.find({city: {$ne: "Nashville, Indiana"}})

{ "_id" : ObjectId("6055ac0b6a275d2394ced121"), "products" : [ ], "name" : "Gospel Flat Farms", "city" : "Bolinas", "email" : "[email protected]", "__v" : 0 }

{ "_id" : ObjectId("605a31baa3e92a0f34774c88"), "products" : [ ObjectId("6157638db579ab008019c4c1") ], "name" : "Star Route Farms", "city" : "Pennysylvania", "email" : "[email protected]", "__v" : 1 }

{ "_id" : ObjectId("61576604ee49ac2db8adc6f0"), "products" : [ ], "name" : "Scotman's Greenlands", "city" : "Stratford-upon-Avon", "email" : "[email protected]", "__v" : 0 }

{ "_id" : ObjectId("61576681ee49ac2db8adc6f1"), "products" : [ ], "name" : "Huckleberry Acres", "city" : "Warwickshire, England", "email" : "[email protected]", "__v" : 0 }

Here, we have successfully found four farms in our database that are not in Nashville, Indiana.

Finding Arrays that are Not Blank Using $ne Operator in MongoDB

To check if an array field is not empty or blank in our MongoDB database, then we can simply take the help of the $ne operator to look up such documents for us. Now, let’s say we want to see only those farms that are actually selling at least one product, meaning, whose “products” field which is an array, is not blank or empty.

Simply follow the below command to see how we can do this:

> db.farms.find({products: {$ne: []}})

{ "_id" : ObjectId("6054f5397c7f6b1274b80939"), "products" : [ ObjectId("6054f5587c7f6b1274b8093a"), ObjectId("6055e998b1016a256057d538") ], "name" : "Lazy Sloth Holdings", "city" : "Nashville, Indiana", "email" : "[email protected]", "__v" : 2 }

{ "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"), "products" : [ ObjectId("615763b1b579ab008019c4c3") ], "name" : "William's Ranch", "city" : "Nashville, Indiana", "email" : "[email protected]", "__v" : 1 }

{ "_id" : ObjectId("605a31baa3e92a0f34774c88"), "products" : [ ObjectId("6157638db579ab008019c4c1") ], "name" : "Star Route Farms", "city" : "Pennysylvania", "email" : "[email protected]", "__v" : 1 }

Updating Documents Using $ne Operator in MongoDB

Let us again take the same sample database and collection. This time we want to update documents using the $ne operator in MongoDB. Let us follow the steps demonstrated below to achieve the same.

  • Let’s say we wish to change the city of all farms to “Kottayam, Kerala”, except for “Lazy Sloth Holdings”.
> db.farms.updateMany({name: {$ne: "Lazy Sloth Holdings"}}, {$set: {city: "Kottayam, Kerala"}})

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 4 }
  • Let us check what all our farms now look like:
> db.farms.find({})

{ "_id" : ObjectId("6054f5397c7f6b1274b80939"), "products" : [ ObjectId("6054f5587c7f6b1274b8093a"), ObjectId("6055e998b1016a256057d538") ], "name" : "Lazy Sloth Holdings", "city" : "Fairfax, Virginia", "email" : "[email protected]", "__v" : 2 }

{ "_id" : ObjectId("6055ac0b6a275d2394ced121"), "products" : [ ], "name" : "Gospel Flat Farms", "city" : "Kottayam, Kerala", "email" : "[email protected]", "__v" : 0 }

{ "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"), "products" : [ ObjectId("615763b1b579ab008019c4c3") ], "name" : "William's Ranch", "city" : "Kottayam, Kerala", "email" : "[email protected]", "__v" : 1 }

{ "_id" : ObjectId("605a31baa3e92a0f34774c88"), "products" : [ ObjectId("6157638db579ab008019c4c1") ], "name" : "Star Route Farms", "city" : "Kottayam, Kerala", "email" : "[email protected]", "__v" : 1 }

{ "_id" : ObjectId("61576604ee49ac2db8adc6f0"), "products" : [ ], "name" : "Scotman's Greenlands", "city" : "Kottayam, Kerala", "email" : "[email protected]", "__v" : 0 }

{ "_id" : ObjectId("61576681ee49ac2db8adc6f1"), "products" : [ ], "name" : "Huckleberry Acres", "city" : "Kottayam, Kerala", "email" : "h[email protected]", "__v" : 0 }

Read More: Ultimate 2021 Guide to Using the $in Operator in MongoDB

Conclusion

This guide explains how you can use the $ne operator that is, the inequality operator in MongoDB, and perform various operations with it.

Noteworthy References

MongoDB Docs

GeeksforGeeks

SO Answer

Aneesha S
Aneesha S
Articles: 173