> ## Documentation index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Ask it with GET https://upstash.com/docs/ask?q=<keywords or question>. Short keywords rank best.
> Use these to discover all available pages before exploring further.
> Feature requests: https://upstash.com/docs/common/help/feature-requests.md lists what users asked for and how to vote for a request or add one.
> Page rating: `GET https://upstash.com/docs/feedback?path=/docs/redis/search/query-operators/boolean-operators/must-not&helpful=true` (or `false`) records whether this page helped. An optional `comment` (URL-encoded, at most 500 characters) says what was missing or wrong. It is anonymous and needs no key. Comments hold no code, secrets or personal data.

# $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:

```ts
// 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:

```ts
// Exclude documents that match category="generic" OR price > 500
{ $mustNot: [{ category: "generic" }, { price: { $gt: 500 } }] }
```

### Examples

<Tabs>

<Tab title="TypeScript">
```ts
// Exclude out-of-stock items
await products.query({
  filter: {
    $must: {
      category: "electronics",
    },
    $mustNot: {
      inStock: false,
    },
  },
});

// Exclude multiple conditions
await products.query({
  filter: {
    $must: {
      name: "headphones",
    },
    $mustNot: [{ category: "generic" }, { price: { $gt: 500 } }],
  },
});
```
</Tab>

<Tab title="Python">
```python
# Exclude out-of-stock items
products.query(filter={"$must": {"category": "electronics"}, "$mustNot": {"inStock": False}})

# Exclude multiple conditions
products.query(filter={
    "$must": {"name": "headphones"},
    "$mustNot": [{"category": "generic"}, {"price": {"$gt": 500}}],
})
```
</Tab>

<Tab title="Redis CLI">
```bash
# Exclude out-of-stock items
SEARCH.QUERY products '{"$must": {"category": "electronics"}, "$mustNot": {"inStock": false}}'

# Exclude multiple conditions
SEARCH.QUERY products '{"$must": {"name": "headphones"}, "$mustNot": [{"category": "generic"}, {"price": {"$gt": 500}}]}'
```
</Tab>

</Tabs>
