|
| 1 | +--- |
| 2 | +code: true |
| 3 | +type: page |
| 4 | +title: updateByQuery |
| 5 | +description: Updates documents matching query |
| 6 | +--- |
| 7 | + |
| 8 | +# updateByQuery |
| 9 | + |
| 10 | +Updates documents matching the provided search query. |
| 11 | + |
| 12 | +Kuzzle uses the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl.html) syntax. |
| 13 | + |
| 14 | +An empty or null query will match all documents in the collection. |
| 15 | + |
| 16 | +<br/> |
| 17 | + |
| 18 | +```js |
| 19 | +updateByQuery(index, collection, searchQuery, changes, [options]) |
| 20 | +``` |
| 21 | + |
| 22 | +| Argument | Type | Description | |
| 23 | +| ------------------ | -------------------------------------------- | --------------- | |
| 24 | +| `index` | <pre>string</pre> | Index name | |
| 25 | +| `collection` | <pre>string</pre> | Collection name | |
| 26 | +| `searchQuery` | <pre>object</pre> | Query to match | |
| 27 | +| `changes` | <pre>object</pre> | Partial changes to apply to the documents | |
| 28 | +| `options` | <pre>object</pre> | Optional parameters | |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +### options |
| 33 | + |
| 34 | +Additional query options. |
| 35 | + |
| 36 | +| Options | Type<br/>(default) | Description | |
| 37 | +| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------- | |
| 38 | +| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) | |
| 39 | +| `source` | <pre>boolean</pre><br/>(`false`)| If true, returns the updated document inside the response |
| 40 | + |
| 41 | +## Resolves |
| 42 | + |
| 43 | +Returns an object containing 2 arrays: `successes` and `errors` |
| 44 | + |
| 45 | +Each updated document is an object of the `successes` array with the following properties: |
| 46 | + |
| 47 | +| Property | Type | Description | |
| 48 | +|------------- |--------------------------------------------- |--------------------------------- | |
| 49 | +| `_source` | <pre>object<String, Object></pre> | Updated document (if `source` option set to true) | |
| 50 | +| `_id` | <pre>string</pre> | ID of the udated document | |
| 51 | +| `_version` | <pre>number</pre> | Version of the document in the persistent data storage | |
| 52 | +| `status` | <pre>number</pre> | HTTP status code | |
| 53 | + |
| 54 | +Each errored document is an object of the `errors` array with the following properties: |
| 55 | + |
| 56 | +| Property | Type | Description | |
| 57 | +|------------- |--------------------------------------------- |--------------------------------- | |
| 58 | +| `document` | <pre>object<String, Object></pre> | Document that causes the error | |
| 59 | +| `status` | <pre>number</pre> | HTTP error status | |
| 60 | +| `reason` | <pre>string</pre> | Human readable reason | |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +<<< ./snippets/update-by-query.js |
0 commit comments