$and

The `$and` operator performs a logical AND operation on an array of expressions and retrieves documents that satisfy all the expressions.

Syntax

{
  $and: [{
    <expression1>
  }, {
    <expression2>
  }, ..., {
    <expressionN>
  }]
}

Parameters

expressionobjectrequired

An array of expressions that must all be true for a document to be included in the results

Examples

Filter stores by staff conditions

This query filters for stores where the number of full-time employees is greater than 10 and part-time employees is less than 15 using the `$and` operator. It projects only the `name` and `staff` fields and limits the result to three records.

Query:

db.stores.find({
    $and: [{
        "staff.employeeCount.fullTime": {
            $gt: 10
        }
    }, {
        "staff.employeeCount.partTime": {
            $lt: 15
        }
    }]
}, {
    "name": 1,
    "staff": 1
}).limit(3)

Output:

[
    {
        "_id": "e60c807b-d31c-4903-befb-5d608f260ba3",
        "name": "Wide World Importers | Appliance Emporium - Craigfort",
        "staff": {
            "totalStaff": {
                "fullTime": 11,
                "partTime": 8
            }
        }
    },
    {
        "_id": "70032165-fded-47b4-84a3-8d9c18a4d1e7",
        "name": "Northwind Traders | Picture Frame Bazaar - Lake Joesph",
        "staff": {
            "totalStaff": {
                "fullTime": 14,
                "partTime": 0
            }
        }
    },
    {
        "_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
        "name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
        "staff": {
            "totalStaff": {
                "fullTime": 16,
                "partTime": 8
            }
        }
    }
]

Evaluate multiple conditions

This query finds stores that have both total sales greater than 100,000 and more than 30 total staff members.

Query:

db.stores.aggregate([
  {
    $project: {
      name: 1,
      totalSales: "$sales.totalSales",
      totalStaff: {
        $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"]
      },
      meetsHighPerformanceCriteria: {
        $and: [
          { $gt: ["$sales.totalSales", 100000] },
          { $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 30] }
        ]
      }
    }
  },
  { $limit: 2 }
])

Output:

[
 {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "totalStaff": 31,
    "meetsHighPerformanceCriteria": false
  },
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "totalStaff": 27,
    "meetsHighPerformanceCriteria": false
  }
]

Related