> For the complete documentation index, see [llms.txt](https://fivesobes.gitbook.io/nexorm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fivesobes.gitbook.io/nexorm/methods/updater-methods/usdupsert.md).

# $upsert

Insert a new record or update an existing record in the table based on the provided conditions. The $upsert method ensures that if a matching record exists, it will be updated; otherwise, a new record will be created. This method is useful for operations where you want to ensure data consistency without performing separate insert and update queries.

| Query          | Parameter's                                                                                                                          |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| **`$where`**   | <mark style="color:yellow;">**`<StaticProps>`**</mark>                                                                               |
| **`$update`**  | <mark style="color:blue;">⇒</mark> [<mark style="color:yellow;">**`<UpdateOptions>`**</mark>](/nexorm/operators/update-operators.md) |
| **`$rules`**   | <mark style="color:blue;">⇒</mark> [<mark style="color:orange;">**`<RulesOptions>`**</mark>](/nexorm/operators/rules-operators.md)   |
| **`$options`** | <mark style="color:blue;">⇒</mark> [<mark style="color:green;">**`<FindOptions>`**</mark>](broken://pages/09lM7s4YrcyDgm9Uz8Ri)      |

```typescript
var updateOrCreate = await Member.$upsert({
    $where: {
        badges: {
            $in: [['verified']]
        }
    },
    $update: {
        $set: {
            badges: ['Special']
        }
    },
    $options: {
        $multi: false
    },
    $rules: {
        badges: {
            $required: true
        }
    }
});
```
