In the next couple of tutorials, we are going to learn more about MongoDB query language, and it’s different operators. MongoDB query language has been designed in such a way that it preserves most of the query power which is fundamental to RDBMS world. This gives us the freedom to write ad-hoc and dynamic queries using the rich set of operators provided by MongoDB. Let us start learning about MongoDB operators.


We are going to learn MongoDB query operators by doing hands-on practice. I Assume that you have MongoDB already installed. We will also need some test dataset for running our queries. If you do not have any test dataset, then you can download a test dataset from here. This has data for some cities along with its location, population, and state. Once the download is complete, you need to extract this file

We can use MongoDB import command to get this database on MongoDB server.

This commands loads data from a zips.json file to the city database. If the city database is not present, then this command creates a city database first.

Comparision operators

Comparision operators are used to filtering data from MongoDB collection based on some criteria.  Some of the basic comparison operators are greater than, less than, equal to, not.  Lets us write some queries and see the usage of these operators.


For example, we have to find cities which has a population more than 80000.  Then we can write the following query.

$gte and $lte

We can see query has returned cities which has a population more than 80000. We can combine multiple comparison operators in the query. Say we need to find cities which has a population more and equal to 80000 and less than and equal to 85000


We can also use equal to operator in queries, say in above query we want cities which are in new york then we can get those cities using the following query.


This is the opposite of the equal operator. In above case say we want cities which are not in new york state.

Plese, remember there is catch while using not equal to operator. It will return cities for which state filed is not defined along with states which are not New York. As MongoDB use free form schema, it is possible that for some documents there is no state filed provided.

$in and $nin

These are similar to equal and not equal operator.  The difference is we can provide a list of things instead of one value in the condition statement. For example,

Element operators

As we have discussed that, MongoDB has a flexible data model. So it is possible that all documents in a collection may not have all fields or field values for one single filed may have different data types. For checking such conditions and document shape, we can use element operators.


Say, we want to find out all documents where state value is not present then we can use the following query

As our collection has all cities with their state name, we do not have any result for this query.


We can use a type operator to find fields having a particular data type.


We have seen some of the operators used in MongoDB. In next article, we are going to learn remaining set of operators. Until then, Keep learning !!!

Leave a Reply

Close Menu