|
| 1 | +--- |
| 2 | +code: true |
| 3 | +type: page |
| 4 | +title: mWrite |
| 5 | +description: Creates or replaces multiple documents directly into the storage engine. |
| 6 | +--- |
| 7 | + |
| 8 | +# mWrite |
| 9 | + |
| 10 | +<SinceBadge version="6.2.0" /> |
| 11 | + |
| 12 | +<SinceBadge version="Kuzzle 1.8.0" /> |
| 13 | + |
| 14 | +Creates or replaces multiple documents directly into the storage engine. |
| 15 | + |
| 16 | +This is a low level route intended to bypass Kuzzle actions on document creation, notably: |
| 17 | + - check [document validity](/core/1/guides/essentials/data-validation), |
| 18 | + - add [kuzzle metadata](/core/1/guides/essentials/document-metadata), |
| 19 | + - trigger [realtime notifications](/core/1/guides/essentials/real-time) (unless asked otherwise) |
| 20 | + |
| 21 | +<br/> |
| 22 | + |
| 23 | +```js |
| 24 | +mWrite (index, collection, documents, [options]) |
| 25 | +``` |
| 26 | + |
| 27 | +<br/> |
| 28 | + |
| 29 | +| Argument | Type | Description | |
| 30 | +| ------------ | ----------------- | -------------------- | |
| 31 | +| `index` | <pre>string</pre> | Index name | |
| 32 | +| `collection` | <pre>string</pre> | Collection name | |
| 33 | +| `documents` | <pre>object[]</pre> | Array of objects representing the documents | |
| 34 | +| `options` | <pre>object</pre> | Query options | |
| 35 | + |
| 36 | +### documents |
| 37 | + |
| 38 | +An array of objects. Each object describes a document to create or replace, by exposing the following properties: |
| 39 | + - `_id`: document unique identifier (optional) |
| 40 | + - `body`: document content |
| 41 | + |
| 42 | +### options |
| 43 | + |
| 44 | +Additional query options |
| 45 | + |
| 46 | +| Property | Type<br/>(default) | Description | |
| 47 | +| ---------- | ------------------------------- | ------------------ | |
| 48 | +| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again | |
| 49 | +| `notify` | <pre>boolean</pre><br/>(`false`) | if set to true, Kuzzle will trigger realtime notifications | |
| 50 | +| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) | |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Resolves |
| 55 | + |
| 56 | +Returns a `hits` array, containing the list of created documents, in the same order than the one provided in the query. |
| 57 | + |
| 58 | +Each created document is an object with the following properties: |
| 59 | + |
| 60 | +| Name | Type | Description | |
| 61 | +| --------- | ----------------- | ------------------- | |
| 62 | +| `_id` | <pre>string</pre> | ID of the newly created document | |
| 63 | +| `_version` | <pre>number</pre> | Version of the document in the persistent data storage | |
| 64 | +| `_source` | <pre>object</pre> | Created document | |
| 65 | + |
| 66 | +If one or more document creations fail, the promise is rejected and the `error` object contains a [partial error](/core/1/api/essentials/errors/#partialerror) error. |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +<<< ./snippets/mWrite.js |
0 commit comments