createDocument

// Using callbacks (NodeJS or Web Browser)
kuzzle
  .collection('collection', 'index')
  .createDocument('foobar', {title: 'foo', content: 'bar'}, {ifExist: 'replace'}, function (err, res) {
    // callback called once the create action has been completed
    // => the result is a Document object
  });

// Using promises (NodeJS only)
kuzzle
 .collection('collection', 'index')
 .createDocumentPromise('foobar', {title: 'foo', content: 'bar'}, {ifExist: 'replace'})
 .then(res => {
   // promise resolved once the create action has been completed
   // => the result is a Document object
 });
Document myDocument = new Document(collection);
myDocument.setContent("title", "foo");
myDocument.setContent("content", "bar");

Options opts = new Options();
opts.setIfExist = "replace";

kuzzle
  .collection("collection", "index")
  .createDocument(myDocument, new ResponseListener<Document>() {
    @Override
    public void onSuccess(Document object) {
      // callback called once the create action has been completed
      // => the result is a Document object
    }

    @Override
    public void onError(JSONObject error) {
      // Handle error
    }
  });
<?php

use \Kuzzle\Kuzzle;
use \Kuzzle\Document;

$documentId = 'foobar';
$documentContent = [
  'title' => 'foo',
  'content' => 'bar'
];

$kuzzle = new Kuzzle('localhost');
$dataCollection = $kuzzle->collection('collection', 'index');

try {
  $document = $dataCollection->createDocument($documentContent, $documentId);

  // $document instanceof Document
}
catch (ErrorException $e) {

}

Create a new document in Kuzzle and resolves to a Document object.


createDocument(Document, [options], [callback])

ArgumentsTypeDescription
DocumentobjectDocument object
optionsJSON objectOptional parameters
callbackfunctionOptional callback

createDocument([id], content, [options], [callback])

ArgumentsTypeDescription
idstringOptional document identifier
contentJSON objectContent of the document to create
optionsJSON objectOptional parameters
callbackfunctionOptional callback

Options

OptionTypeDescriptionDefault
volatileJSON objectAdditional information passed to notifications to other usersnull
queuablebooleanMark this request as (not) queuabletrue
refreshstringIf set to wait_for, Kuzzle will wait the persistence layer indexation to return (available with Elasticsearch 5.x and above)undefined
ifExiststringIf the same document already exists: resolves to an error if sets to error. Replaces the existing document if set to replacefalse

Return value

Returns the Collection object to allow chaining.


Callback response

Resolves to a Document object containing the newly created document.