|
| 1 | +--- |
| 2 | +code: true |
| 3 | +type: page |
| 4 | +title: mUpsert |
| 5 | +description: Update documents |
| 6 | +--- |
| 7 | + |
| 8 | +# mUpsert |
| 9 | + |
| 10 | +<SinceBadge version="Kuzzle 2.11.0"/> |
| 11 | +<SinceBadge version="7.7.1"/> |
| 12 | + |
| 13 | +Applies partial changes to multiple documents. If a document doesn't already exist, a new document is created. |
| 14 | + |
| 15 | +You can set the `retryOnConflict` optional argument (with a retry count), to tell Kuzzle to retry the failing updates the specified amount of times before rejecting the request with an error. |
| 16 | + |
| 17 | +<br/> |
| 18 | + |
| 19 | +```js |
| 20 | +mUpsert(index, collection, documents, [options]); |
| 21 | +``` |
| 22 | + |
| 23 | +| Argument | Type | Description | |
| 24 | +|--------------|---------------------|------------------------------| |
| 25 | +| `index` | <pre>string</pre> | Index name | |
| 26 | +| `collection` | <pre>string</pre> | Collection name | |
| 27 | +| `documents` | <pre>object[]</pre> | Array of documents to update | |
| 28 | +| `options` | <pre>object</pre> | Query options | |
| 29 | + |
| 30 | + |
| 31 | +### documents |
| 32 | + |
| 33 | +`documents` is an array of object which each object representing a document. Fields `_id` and `changes` is always mandatory while `default` is optional. |
| 34 | +Example: |
| 35 | + |
| 36 | +```js |
| 37 | +[ |
| 38 | + { |
| 39 | + "_id": "<documentId>", |
| 40 | + "changes": { |
| 41 | + // document partial changes |
| 42 | + }, |
| 43 | + "default": { |
| 44 | + // optional: document fields to add to the "update" part if the document |
| 45 | + // is created |
| 46 | + } |
| 47 | + }, |
| 48 | + { |
| 49 | + "_id": "<anotherDocumentId>", |
| 50 | + "changes": { |
| 51 | + // document partial changes |
| 52 | + }, |
| 53 | + } |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | +### Options |
| 58 | + |
| 59 | +Additional query options |
| 60 | + |
| 61 | +| Options | Type<br/>(default) | Description | |
| 62 | +|-------------------|----------------------------------|------------------------------------------------------------------------------------------| |
| 63 | +| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again | |
| 64 | +| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) | |
| 65 | +| `retryOnConflict` | <pre>int</pre><br/>(`0`) | The number of times the database layer should retry in case of version conflict | |
| 66 | +| `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> | |
| 67 | + |
| 68 | +## Resolves |
| 69 | + |
| 70 | +Returns an object containing 2 arrays: `successes` and `errors` |
| 71 | + |
| 72 | +Each updated document is an object of the `successes` array with the following properties: |
| 73 | + |
| 74 | +| Name | Type | Description | |
| 75 | +|------------|-------------------|--------------------------------------------------------| |
| 76 | +| `_id` | <pre>string</pre> | Document ID | |
| 77 | +| `status` | <pre>number</pre> | HTTP error status | |
| 78 | +| `created` | <pre>boolean</pre>| `true` if the document has been created | |
| 79 | +| `_version` | <pre>number</pre> | Version of the document in the persistent data storage | |
| 80 | +| `_source` | <pre>object</pre> | Document content | |
| 81 | + |
| 82 | +Each errored document is an object of the `errors` array with the following properties: |
| 83 | + |
| 84 | +| Name | Type | Description | |
| 85 | +|------------|-------------------|-------------------------------| |
| 86 | +| `document` | <pre>object</pre> | Document that cause the error | |
| 87 | +| `status` | <pre>number</pre> | HTTP error status | |
| 88 | +| `reason` | <pre>string</pre> | Human readable reason | |
| 89 | + |
| 90 | +## Usage |
| 91 | + |
| 92 | +<<< ./snippets/m-upsert.js |
0 commit comments