Boolean Operators

$must

The $must operator requires all specified conditions to match. Documents are only included in results if they satisfy every condition within the $must clause. This implements logical AND behavior.

When you specify multiple field conditions at the top level of a query without any boolean operator, they are implicitly wrapped in a $must. These queries are equivalent:

// Implicit must{ name: "headphones", inStock: true }// Explicit $must{ $must: { name: "headphones", inStock: true } }

Syntax Options

The $must operator accepts either an object or an array:

  • Object syntax: Each key-value pair is a condition that must match
  • Array syntax: Each element is a separate condition object that must match

Array syntax is useful when you have multiple conditions on the same field or when building queries programmatically.

Examples

// Explicit $mustawait products.query({  filter: {    $must: [{ name: "headphones" }, { inStock: true }],  },});// Equivalent implicit formawait products.query({  filter: {    name: "headphones",    inStock: true,  },});// $must with object syntaxawait products.query({  filter: {    $must: {      name: "headphones",      inStock: true,    },  },});
# Explicit $mustproducts.query(filter={"$must": [{"name": "headphones"}, {"inStock": True}]})# Equivalent implicit formproducts.query(filter={"name": "headphones", "inStock": True})# $must with object syntaxproducts.query(filter={"$must": {"name": "headphones", "inStock": True}})
# Explicit $mustSEARCH.QUERY products '{"$must": [{"name": "headphones"}, {"inStock": true}]}'# Equivalent implicit formSEARCH.QUERY products '{"name": "headphones", "inStock": true}'# $must with object syntaxSEARCH.QUERY products '{"$must": {"name": "headphones", "inStock": true}}'
Loading search…