create

Creates a new document in the persistent data storage.

Throws an error if the document already exists.

The optional parameter refresh can be used with the value wait_for in order to wait for the document to be indexed (indexed documents are available for search).


Copied to clipboard!
create(index, collection, document, [id], [options]);
Argument Type Description
index
string
Index name
collection
string
Collection name
document
object
Document content
id
string
Optional document ID
options
object
Query options

Options

Additional query options

Options Type
(default)
Description
queuable
boolean

(true)
If true, queues the request during downtime, until connected to Kuzzle again
refresh
string

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

Resolves

Resolves to an object containing the document creation result.

Name Type Description
_id
string
ID of the newly created document
_version
number
Version of the document in the persistent data storage
_source
object
Created document

Usage

Copied to clipboard!
try {
  const response = await kuzzle.document.create(
    'nyc-open-data',
    'yellow-taxi',
    { lastName: 'Eggins' },
    'some-id'
  );

  if (response._id === 'some-id') {
    console.log('Success');
  }
  /*
    response =
    { _index: 'nyc-open-data',
      _type: 'yellow-taxi',
      _id: 'some-id',
      _version: 1,
      result: 'created',
      _shards: { total: 2, successful: 1, failed: 0 },
      created: true,
      _source:
        { lastName: 'Eggins',
          _kuzzle_info:
            { author: '-1',
              createdAt: 1537443212089,
              updatedAt: null,
              updater: null,
              active: true,
              deletedAt: null } } }
   */
} catch (error) {
  console.error(error.message);
}