> This page location: /docs.md > /docs/development.md > /docs/manual/reference/mql.md > /docs/manual/reference/mql/update.md > /docs/manual/reference/mql/update/field.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $setOnInsert (update operator)

## Definition

If an update operation with [upsert: true](https://www.mongodb.com/docs/manual/reference/method/db.collection.update.md#std-label-upsert-parameter) results in an insert of a document, then [`$setOnInsert`](https://www.mongodb.com/docs/manual/reference/operator/update/setoninsert.md#mongodb-update-up.-setOnInsert) assigns the specified values to the fields in the document. If the update operation does not result in an insert, [`$setOnInsert`](https://www.mongodb.com/docs/manual/reference/operator/update/setoninsert.md#mongodb-update-up.-setOnInsert) does nothing.

You can specify the `upsert` option for:

- [`db.collection.updateOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateone.md#mongodb-method-db.collection.updateOne)

- [`db.collection.updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updatemany.md#mongodb-method-db.collection.updateMany)

- [`db.collection.findOneAndUpdate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findoneandupdate.md#mongodb-method-db.collection.findOneAndUpdate)

```javascript
db.collection.updateOne(
   <query>,
   { $setOnInsert: { <field1>: <value1>, ... } },
   { upsert: true }
)
```

To specify a `<field>` in an embedded document or in an array, use [dot notation.](https://www.mongodb.com/docs/manual/core/document.md#std-label-document-dot-notation)

## Behavior

Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See [Update Operators Behavior](https://www.mongodb.com/docs/manual/reference/mql/update.md#std-label-update-operators-processing-order) for details.

Starting in MongoDB 5.0, [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) no longer raises an error when you use an update operator like [`$setOnInsert`](https://www.mongodb.com/docs/manual/reference/operator/update/setoninsert.md#mongodb-update-up.-setOnInsert) with an empty operand expression ( `{ }` ). An empty update results in no changes and no [oplog](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-oplog) entry is created (meaning that the operation is a no-op).

## Example

The `products` collection contains no documents.

Insert a new document using [`db.collection.updateOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateone.md#mongodb-method-db.collection.updateOne) the [upsert: true](https://www.mongodb.com/docs/manual/reference/method/db.collection.update.md#std-label-upsert-parameter) parameter.

```javascript
db.products.updateOne(
  { _id: 1 },
  {
     $set: { item: "apple" },
     $setOnInsert: { defaultQty: 100 }
  },
  { upsert: true }
)
```

MongoDB uses `<query>` to create a new document with `_id: 1`. [`$setOnInsert`](https://www.mongodb.com/docs/manual/reference/operator/update/setoninsert.md#mongodb-update-up.-setOnInsert) updates the document as specified.

The `products` collection contains the newly-inserted document:

```javascript
{ "_id" : 1, "item" : "apple", "defaultQty" : 100 }
```

When the [upsert](https://www.mongodb.com/docs/manual/reference/method/db.collection.update.md#std-label-upsert-parameter) parameter is `true` [`db.collection.updateOne()`:](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateone.md#mongodb-method-db.collection.updateOne)

- creates a new document

- applies the [`$set`](https://www.mongodb.com/docs/manual/reference/operator/update/set.md#mongodb-update-up.-set) operation

- applies the [`$setOnInsert`](https://www.mongodb.com/docs/manual/reference/operator/update/setoninsert.md#mongodb-update-up.-setOnInsert) operation

If [`db.collection.updateOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateone.md#mongodb-method-db.collection.updateOne) matches an existing document, MongoDB only applies the [`$set`](https://www.mongodb.com/docs/manual/reference/operator/update/set.md#mongodb-update-up.-set) operation.

**See also:**

- [`db.collection.updateOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateone.md#mongodb-method-db.collection.updateOne)

- [`db.collection.findOneAndUpdate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findoneandupdate.md#mongodb-method-db.collection.findOneAndUpdate)
