The Wayback Machine - https://web.archive.org/web/20240927195836/https://www.geeksforgeeks.org/mongodb-and-operator-and/
Open In App

MongoDB AND operator ( $and )

Last Updated : 05 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array. You can use this operator in methods like find(), update(), etc. according to your requirements.

  • This operator performs short-circuit evaluation.
  • If the first expression of $and operator evaluates to false, then MongoDB will not evaluate the remaining expressions in the array.
  • You can also use AND operation implicitly with the help of comma(, ).
  • You can use AND operation explicitly (i.e., $and) when the same field or operator specified in multiple expressions.

Syntax: 

{ $and: [ { Expression1 }, { Expression2 }, ..., { ExpressionN } ] }
or
{ { Expression1 }, { Expression2 }, ..., { ExpressionN }}

Examples: In the following examples, we are working with:

Database: GeeksforGeeks Collection: contributor Document: three documents that contain the details of the contributors in the form of field-value pairs.

Matching values using $and operator:

In this example, we are retrieving only those employee’s documents whose branch is CSE and joiningYear is 2018. 

Python3




db.contributor.find({$and: [{branch: "CSE"},
                            {joiningYear: 2018}]}).pretty()


Using $and operator with multiple expressions specifying the same field :

In this example, we are retrieving only those employee’s documents whose branch is CSE. 

Python3




db.contributor.find({$and: [{branch: {$eq: "CSE"}},
                            {branch: {$exists: true}}]}).pretty()


or 

Python3




db.contributor.find({branch: {$eq: "CSE", $exists: true}}).pretty()


Using $and operator with multiple expressions specifying the same operator :

In this example, we are retrieving only those employee’s documents whose satisfy the given conditions. 

Python3




db.contributor.find({$and: [{$or: [{branch: "ECE"}, {joiningYear: 2017}]},
                            {$or: [{"personal.state": "UP"},
                                   {"personal.age": 25}]}]}).pretty()




Previous Article
Next Article

Similar Reads

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass
Pre-requisite: AWS and MongoDB In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass.  If you haven't installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and
4 min read
MongoDB - Delete Multiple Documents Using MongoDB Shell
The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter. The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method
2 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database known for flexibility and scalability. MongoDB Compass offers a graphical interface for managing databases visually while MongoDB Atlas is a cloud-based service for automated management and scaling of MongoDB databases. In this article, We will learn about the in-depth understanding of the difference between the
5 min read
MongoDB Query and Projection Operator
MongoDB query operators are essential tools for interacting with data stored in MongoDB collections. Similar to SQL, MongoDB offers a range of operators to perform various operations such as comparisons, logical evaluations, and array manipulations. These operators allow users to efficiently query, filter, and manipulate data based on specific cond
11 min read
MongoDB NOT operator ( $not )
MongoDB provides different types of logical query operators and $not operator is one of them. This operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression. It also includes those documents that don't contain the field. You can
2 min read
MongoDB OR operator ( $or )
MongoDB provides different types of logical query operators and $or operator is one of them. This operator is used to perform logical OR operation on the array of two or more expressions and select or retrieve only those documents that match at least one of the given expression in the array. You can use this operator in methods like find(), update(
2 min read
MongoDB NOR operator ( $nor )
MongoDB provides different types of logical query operators and $nor operator is one of them. This operator is used to perform logical NOR operation on the array of one or more expressions and select or retrieve only those documents that do not match all the given expression in the array. You can use this operator in methods like find(), update(),
2 min read
MongoDB - Current Date Operator ($currentDate)
MongoDB provides different types of field update operators to update the values of the fields of the documents and $currentDate operator is one of them. This operator is used to set the value of a field to the current date (either as a timestamp or as a Date). The default type of $currentDate operator is a Date. This operator can also work with emb
2 min read
MongoDB - $ne Operator
MongoDB $ne or "not equals" operator is one of the comparison operators. The $ne operator selects those documents where the field value is not equal to the given value. It also includes those documents that do not contain the specified field. You can use this operator in methods like find(), update(), etc. as per your requirement. Syntax{field: {$n
2 min read
MongoDB - $nin Operator
MongoDB $nin or " not in" is one of the comparison query operators. The $nin operator selects those documents where the field value is not equal to any of the given values in the array and the field that does not exist. You can use this operator in methods like find(), update(), etc. according to your requirements. Syntax{field: {$nin: [value1, val
2 min read
MongoDB - $max Operator
MongoDB $max or maximum operator is one of the field update operators. $max operator updates the field with the specified value if the specified value is greater than the current value. Important Points This operator will compare the values of different data types according to the BSON comparison order.You can also use this operator in embedded/nes
3 min read
MongoDB - $min operator
MongoDB $min or minimum operator is one of the field update operators. $min operator updates the field with the specified value if the specified value is less than the current value. The $min operator will compare the values of different data types according to the BSON comparison order. This operator can also be used in embedded/nested documents u
3 min read
MongoDB - $inc Operator
MongoDB $inc or increment operator is a type of field update operator. $inc operator increases the values of the fields to the specified amount or increases the field by the given value. Important PointsThis operator accepts positive and negative values.If the given field does not exist, then this operator will create a field and set the value of t
2 min read
MongoDB - $lt Operator
MongoDB $lt operator or less than operator is one of the comparison operators. The $lt operator selects the documents with a value less than (<) the given value. You can use this operator in methods like, find(), update(), etc. as per your requirements. Syntax{field: {$lt: value}}MongoDB $lt Operator ExampleIn the following examples, we are work
2 min read
MongoDB - $lte Operator
MongoDB $lte Operator is one of the comparison operators. $lte operator selects those documents where the field value is less than equal to (<=) the given value. This operator can be used in methods like find(), update(), etc. according to your requirements.. Syntax{field: {$lte: value}}MongoDB $lte Operator ExampleIn the following examples, we
2 min read
MongoDB $gt Operator
MongoDB $gt operator selects documents where the value of a field is greater than a specified value. $gt Operator in MongoDBThe $gt operator in MongoDB is a comparison operator. It is used to find documents where the value in the field is greater than(>) a given value. This operator can be used with other methods like find, update, etc. accordin
2 min read
MongoDB - $gte Operator
MongoDB $gte or "greater than equals to" operator is one of the comparison operators. $gte operator selects those documents where the field value is greater than equals to(>=) the given value. This operator can be used in methods (like, find(), update(), etc.) according to your requirements. Syntax{field: {$gte: value}}MongoDB $gte Operator Exam
2 min read
MongoDB - $eq Operator
MongoDB $eq operator or equality operator is a comparison operator. The $eq operator matches documents where the value of the field is equal to the specified value. Important Points: If the given value is a document, then the order of the fields in the document is important.If the given value is an array, then MongoDB matches the documents where th
2 min read
MongoDB - Rename Operator ($rename)
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure. This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in
4 min read
MongoDB - $setOnInsert Operator
The $setOnInsert operator in MongoDB is a powerful tool used in updating operations with the upsert option. It allows us to specify values that should be set only when a new document is inserted. In this article, we will learn about the $setOnInsert Operator in MongoDB in detail and so on. MongoDB $setOnInsert OperatorMongoDB $setOnInsert Operator
4 min read
MongoDB - Positional Operator ($)
MongoDB provides a variety of array update operators to modify the values within array fields in documents. One such operator is the positional operator $. This operator allows us to update an element in an array without explicitly specifying the position of that element. In this article, We will learn about the Positional Operator($) in MongoDB in
4 min read
MongoDB $push Operator
The $push operator in MongoDB is a powerful tool used to update arrays within documents. It appends the entire array as a single element. To add each element of the array individually, the $each modifier can be used alongside $push. In this article, We will learn about the MongoDB $push Operator by understanding various examples in detail. $push Op
5 min read
MongoDB - $pullAll Operator
MongoDB $pullAll operator is a crucial tool for efficiently managing arrays within MongoDB documents. It allows users to remove all instances of specified values from an array and provides a direct way to update array fields. In this article, We will learn about the MongoDB $pullAll Operator by understanding various examples in detail. MongoDB $pul
3 min read
MongoDB - $mul Operator
MongoDB $mul operator is a powerful update operator used to multiply the value of a field by a specified number. This operator allows for direct arithmetic operations within the database, making it particularly useful for scenarios that require modifying numeric field values without needing to retrieve the current value first. In this article, We w
4 min read
MongoDB $in Operator
MongoDB $in operator provides a powerful way to query documents based on multiple potential values for a specific field within a single query. In this article, We will learn about the MongoDB $in Operator by understanding various examples in detail. MongoDB $in OperatorThe MongoDB $in operator is used to match documents where the value of a field e
4 min read
MongoDB - $pop Operator
The $pop operator in MongoDB is designed for managing array fields by removing either the first or last element of the array. This operator simplifies the process of maintaining array size and managing data in scenarios where a fixed number of elements is required. In this article, We will learn about the MongoDB $pop Operator in detail by understa
4 min read
MongoDB $pull Operator
The $pull operator in MongoDB is a powerful update operator used to remove all instances of a specified value or values from an array. This operator is particularly useful for modifying arrays within documents without retrieving and updating the entire array manually. In this article, We will learn about the MongoDB $pull Operator by understanding
5 min read
Difference between Oracle and MongoDB
1. Oracle : Oracle is a relational database management system (RDBMS). It was developed by Oracle Corporation in 1980. It is the first database designed for grid computing that provides the most flexible and cost-effective way to manage information and application. It runs on major platforms like Windows, Unix, Linux, and macOS. It is a relational
2 min read
Introduction about Node.js and MongoDB
NoSQL databases are databases that store and retrieve the data that is present in a non-tabular format. Depending on the format of the data stored, NoSQL databases are split into 4 major types: Key-Value Graph database Document-oriented Column family These databases are built to address the shortcomings of traditional RDBMS and provide high perform
4 min read
Difference between RDBMS and MongoDB
MongoDB is an open-source document-oriented database used for high volume data storage. It falls under the classification of a NoSQL database. NoSQL tool means that it doesn't utilize the usual rows and columns. MongoDB uses BSON (document storage format) which is a binary style of JSON documents. Features of MongoDB: Multiple Servers: It can run o
2 min read
Article Tags :