Query operators in MongoDB

AbdulQudus Yunus
2 min readJan 22, 2021

In this post we are going to be looking at various examples on how to query data in mongoDB. MongoDB is a document oriented database, it stores data similarly to JSON objects.

  1. $or

According to the documentation the$or operator performs a logical OR operation on an array of two or expressions and selects the documents that satisfy at least one of the expressions. Example is shown below

2. $in

The $in operator is very similar to the$or operator. It sort out the value of a field equals to any value in the defined array. More in the documentation here. An example using the $in query. The query below and above will return the exact same results. Meaning the $in could serve has the shorthand of the $or query in some cases.

3. $ne

This basically means the not operation. This selects all the documents that is not equal to the specified value, inclusive of when the field is null or the field doesn't exists. The documentation can be found here

4. $nin

According to the documentation this select all the documents where the field value is not in the specified array or the field doesn't exists at all

5. $and

The $and query selects all the documents that satisfy all the given expressions in the array. More can be found in the documentation.

6. $gt and $lt

The $gt selects the documents where the field value is grater than the specified value. The $lt does the opposite, i.e it selects all the documents where the field value is less than the specified value. There are also very similar queries like $gte which selects greater than or equal to and $lte which selects less than or equal to.

The queries operators above are my most used query operators when using mongoDB. There is really no limit of of how you can query in mongoDB you just have to find the query operator that fit your needs. You can find more in the documentation here.

If you found this post interesting or useful pls clap :) or leave a comment below for any addition. Thanks for reading.

--

--