> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# $mustNot

The `$mustNot` operator excludes documents that match any of the specified conditions.

How `$mustNot` works depends on the other conditions next to it:

* **With other conditions, such as `$must` or a field condition**: `$mustNot` filters the matching
  documents. It removes the documents that match its conditions.
* **On its own**: the query matches every document that `$mustNot` does not exclude. All of these
  documents have a score of `0`.
* **Inside `$should`**: `$mustNot` is one of the alternatives. It matches every document it does not
  exclude, with a score of `0`.

```ts theme={"system"}
// Matches all documents except electronics, with a score of 0
{ $mustNot: { category: "electronics" } }

// Matches in-stock documents, except electronics
{ $must: { inStock: true }, $mustNot: { category: "electronics" } }

// Matches headphones, and every document that is not electronics with a score of 0
{ $should: [{ name: "headphones" }, { $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 theme={"system"}
// Exclude documents that match category="generic" OR price > 500
{ $mustNot: [{ category: "generic" }, { price: { $gt: 500 } }] }
```

### Examples

> [tab] TypeScript
>
> ```ts theme={"system"}
> // 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] Python
>
> ```python theme={"system"}
> # 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] Redis CLI
>
> ```bash theme={"system"}
> # 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}}]}'
> ```


## Related topics

- [Upstash Redis Changelog](https://upstash.com/docs/redis/overall/changelog.md)
- [E-commerce Search](https://upstash.com/docs/redis/search/recipes/e-commerce-search.md)
- [Redis Search Troubleshooting](https://upstash.com/docs/redis/search/troubleshooting.md)
- [Redis Search Boolean Operators](https://upstash.com/docs/redis/search/query-operators/boolean-operators/overview.md)
- [User Directory](https://upstash.com/docs/redis/search/recipes/user-directory.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.