> ## Documentation index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Search it with GET https://upstash.com/docs/search?q=<query>.
> Use these 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
// 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
// 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>
