Array Operators

The Array operators in the MongoDB return the result based on the Multiple conditions specified in the array using the array operators.

The Array Operators in the MongoDB are:

Array Operator Description Syntax
$all Returns the documents in the collection which have all the elements specified in the array. { field: { $all: [<value1> , <value2>, ... ] } }
$elemMatch Returns the documents that match all the conditions in the given array of conditions. { field: { $elemMatch: { <query1>, <query2>, ... } } }
$size Returns the documents that satisfy the given size mentioned in the query if the field contains an array of specified size. { field: { $size: value } }

Example of Using $all Operator

Let’s find out the Person whose Age is 23.

Query:

db.count_no.find( {"Age" :{$all : [23] } }, {_id:0} );

Output:

$ALL operator in MongoDB.

Explanation: In the above query, We have used the $all operator along with the Age and it should have all the values in the array and it should be of array type.

Example of Using $elemMatch Operator

Let’s first make some updates in our demo collection. Here we will Insert some data into the count_no database

Query:

db.count_no.insertOne( {"name" : "Harsha", "Age": 24,"Likes" : 2,"Colors":["Red","Green","Blue"] } );

Output:

Inserting the array of elements .

Now, using the $elemMatch operator in MongoDB let’s match the Red colors from a set of Colors.

Query:

db.count_no.find( {"Colors": {$elemMatch: {$eq: "Red" } } },{_id: 0 } );

Output:

Using the elemMatch Operator.

Explanation: The $elemMatch operator is used with the field Colors which is of type array. In the above query, it returns the documents that have the field Colors and if any of the values in the Colors field has “Red” in it.

MongoDB Query and Projection Operator

MongoDB query and projection operators allow users to control queries and results. Query operators help to filter data based on specific conditions. E.g., $eq,$and,$exists, etc.

Projection operators in MongoDB control which fields should be shown in the results. E.g., $project, $slice, $concat, etc.

In this article, we will learn about query and projection operators in MongoDB with detailed explanations and examples. Let’s start by learning the Query operators in MongoDB.

Similar Reads

MongoDB Query Operators

Similar to SQL MongoDB have also some operators to operate on data in the collection. MongoDB query operators check the conditions for the given data and logically compare the data with the help of two or more fields in the document....

Demo MongoDB Database

To understand the Query and Projection Operators in MongoDB we need a database on which we will perform operations. So we have created a collection called count_no with the help of the db.createCollection( ) function....

Types of Query Operators in MongoDB

The Query operators in MongoDB can be further classified into 8 more types. The 8 types of Query Operators in MongoDB are:...

1. Comparison Operators

The comparison operators in MongoDB are used to perform value-based comparisons in queries....

2. Logical Operators

The logical operators in MongoDB are used to filter data based on expressions that evaluate to true or false....

3. Array Operators

The Array operators in the MongoDB return the result based on the Multiple conditions specified in the array using the array operators....

4. Evaluation Operators

The evaluation operators in the MongoDB are used to return the documents based on the result of the given expression....

5. Element Operators

The element operators in the MongoDB return the documents in the collection which returns true if the keys match the fields and datatypes....

6. Bitwise Operators

The Bitwise operators in the MongoDB return the documents in the MongoDB mainly on the fields that have numeric values based on their bits similar to other programming languages....

7. Geospatial Operators

The Geospatial operators in the MongoDB are used mainly with the terms that relate to the data which mainly focuses on the directions such as latitude or longitudes....

8. Comment Operators

The $comment operator in MongoDB is used to write the comments along with the query in the MongoDB which is used to easily understand the data....

MongoDB Projection Operator

The Projection Operator in MongoDB allows us to control which fields are included in the results of a Query. With the help of the Projection Operator, the performance of our Query will be enhanced....

Conclusion

Query and Project operators are used in MongoDB, mainly for retrieving the data from the database based on the given condition. The MongoDB operators play a crucial role in performing the operations such as CRUD operations....