In the last tutorial, we have seen some of MongoDB operators. Lets us look further and learn about remaining MongoDB operators.

Prerequisite

I assume that you have a working installation of MongoDB. If not then you can follow this tutorial installing MongoDB on Windows. We have seen some of the operators in the last tutorial; you can take a look into it here. If you have already gone through it, let us start learning next set of MongoDB operators.

Logical Operators

If we have to specify more than one condition using complex and/or logic, then we have to use logical operators.

$or

When using any logical operator, we specify an array of conditions by which we want to filter records. In our city database, suppose we want to find out cities which are in New York or having a population more than 100000. Then we can write the following query.

This query returns all documents which satisfy either condition state values as NY or population greater than 100000.

$and

We use this operator when we want documents satisfying multiple conditions at once. In above query let us assume that we want cities which are in New York state and has a population more than 100000

We can get the same results using the following query.

Conditions in MongoDB query are implicitly anded together. This will make us wonder that why do we need $and operator then. In some cases, we need to specify same filed with different conditions in the same query. In such cases, we have to use $and operator.

$not and $nor

$not operator is used when we want to invert the result of some conditional statement.  $nor operator is opposite to or operation.

Above query returns documents which have state value other than NY and population less than 100000.

Evaluation operators

$regex

Regex operator is used to find all documents having some pattern matching to our condition. In many cases, we do not have exact condition matching but some pattern like all names starting with N. In such cases, we can use $regex operator.

Above query returns all cities which name start with N.

$expr

Consider the following documents

Now we want to find all documents where spend is more than the budget. For such kind of queries, we can use $expr operator. We can use the following query to get the above result.

Array operators

$all

We use $all operator when we want to match all elements of an array and return only those documents. In the following query, we want to find a document which satisfies given a condition for location coordinates.

$size

We can use this operator for array fields which have the desired length. For example, we want to find array filed having size 2.

As all locations have two values, it returns all documents in our case.

$elemMtach

If we want to match criteria for each element of the array individually, then we have to use $elemMatch operator.

Above query returns all documents where results array has elements in range 80 to 85.

Conclusion

We have learned different MongoDB operators and understand their usage using example queries. Going forward, we are going to use these operators to update and read  MongoDB documents in the future tutorial. See you there soon.

I am passionate about data analytics, machine learning, and artificial intelligence. Recently I have started blogging about my experience while learning these exciting technologies.

Leave a Reply

Close Menu