SDK
SDK Javascript v7.x
2

mWrite #

Available since 6.2.0
Available since Kuzzle 1.8.0

Creates or replaces multiple documents directly into the storage engine.

This is a low level route intended to bypass Kuzzle actions on document creation, notably:


Copied to clipboard!
mWrite (index, collection, documents, [options])

Argument Type Description
index
string
Index name
collection
string
Collection name
documents
object[]
Array of objects representing the documents
options
object
Query options

documents #

An array of objects. Each object describes a document to create or replace, by exposing the following properties:

  • _id: document unique identifier (optional)
  • body: document content

options #

Additional query options

Property Type
(default)
Description
queuable
boolean

(true)
If true, queues the request during downtime, until connected to Kuzzle again
notify
boolean

(false)
if set to true, Kuzzle will trigger realtime notifications
refresh
string

("")
If set to wait_for, waits for the change to be reflected for search (up to 1s)
timeout
number

(-1)
Time (in ms) during which a request will still be waited to be resolved. Set it -1 if you want to wait indefinitely

Resolves #

Returns an object containing 2 arrays: successes and errors

Each created or replaced document is an object of the successes array with the following properties:

Name Type Description
_id
string
Document ID
_version
number
Version of the document in the persistent data storage
_source
object
Document content
created
boolean
True if the document was created

Each errored document is an object of the errors array with the following properties:

Name Type Description
document
object
Document that cause the error
status
number
HTTP error status
reason
string
Human readable reason

Usage #

Copied to clipboard!
try {
  const documents = [
    {
      _id: 'panipokari',
      body: {
        licence: 'B',
        _kuzzle_info: { creator: 'bahi', createdAt: 746186305 }
      }
    },
    {
      body: {
        licence: 'B',
        _kuzzle_info: { creator: 'dahi', createdAt: 835005505 }
      }
    }
  ];
  const result = await kuzzle.bulk.mWrite(
    'nyc-open-data',
    'yellow-taxi',
    documents
  );
  console.log(result);
  /*
  { successes:
    [ { _id: 'panipokari',
      created: true,
      _version: 1,
      _source: {
        "licence": "B",
        "_kuzzle_info": {
          "creator": "bahi",
          "createdAt": 746186305 } },
        _version: 1 },
      { _id: 'AWvlrMIZE7sKlamkyoHr',
        created: true,
        _version: 1,
        _source: {
          "licence": "B",
          "_kuzzle_info": {
            "creator": "dahi",
            "createdAt": 835005505 } },
        _version: 1 } ],
    errors: [] }
  */
  console.log(`Document creator is ${result.successes[1]._source._kuzzle_info.creator}`);
} catch (error) {
  console.error(error.message);
}