Boolean Operators
$mustNot
The $mustNot operator excludes documents that match any of the specified conditions.
It acts as a filter, removing matching documents from the result set.
The $mustNot operator only filters results—it never adds documents to the result set.
This means it must be combined with $must or $should to define which documents to search.
A query with only $mustNot returns no results because there is no base set to filter:
// This returns NO results - nothing to filter{ $mustNot: { category: "electronics" } }// This works - $must provides the base set, $mustNot filters it{ $must: { inStock: true }, $mustNot: { category: "electronics" } }Excluding Multiple Conditions
When $mustNot contains multiple conditions (via array or object), documents matching ANY of those conditions are excluded.
This is effectively an OR within the exclusion:
// Exclude documents that match category="generic" OR price > 500{ $mustNot: [{ category: "generic" }, { price: { $gt: 500 } }] }Examples
// Exclude out-of-stock itemsawait products.query({ filter: { $must: { category: "electronics", }, $mustNot: { inStock: false, }, },});// Exclude multiple conditionsawait products.query({ filter: { $must: { name: "headphones", }, $mustNot: [{ category: "generic" }, { price: { $gt: 500 } }], },});# Exclude out-of-stock itemsproducts.query(filter={"$must": {"category": "electronics"}, "$mustNot": {"inStock": False}})# Exclude multiple conditionsproducts.query(filter={ "$must": {"name": "headphones"}, "$mustNot": [{"category": "generic"}, {"price": {"$gt": 500}}],})# Exclude out-of-stock itemsSEARCH.QUERY products '{"$must": {"category": "electronics"}, "$mustNot": {"inStock": false}}'# Exclude multiple conditionsSEARCH.QUERY products '{"$must": {"name": "headphones"}, "$mustNot": [{"category": "generic"}, {"price": {"$gt": 500}}]}'