<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/xdmp.documentInsert.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>xdmp.documentInsert</api:function-name><api:suggest>xdmp.documentinsert</api:suggest><api:suggest>xdmp</api:suggest><api:suggest>documentinsert</api:suggest><api:function-link mode="xquery" fullname="xdmp:document-insert">/apidoc/8.0/xdmp:document-insert.xml</api:function-link><api:function mode="javascript" name="documentInsert" type="builtin" lib="xdmp" category="UpdateBuiltins" hidden="false" bucket="MarkLogic Built-In Functions" prefix="xdmp" namespace="http://marklogic.com/xdmp" fullname="xdmp.documentInsert"><api:summary>
  Inserts a new document into the database if a document with the
  specified URI does not already exist.  If a document already exists
  at the specified URI, the function replaces the content of the existing
  document with the specified content (the <code xmlns="http://www.w3.org/1999/xhtml">$root</code> parameter)
  as an update operation.  In addition to replacing the content,
  <code xmlns="http://www.w3.org/1999/xhtml">xdmp:document-insert</code> replaces any permissions, collections,
  and quality with the ones specified (or with the default values for these
  parameters, if not explicitly specified).  Also, if a properties
  document exists at the same URI, that properties document (including any
  content it contains) is preserved.
</api:summary><api:params><api:param name="uri" type="xs:string"><api:param-description>
    The URI of the document to be inserted.
  </api:param-description><api:param-name>uri</api:param-name><api:param-type>String</api:param-type></api:param><api:param name="root" type="node()"><api:param-description>
    The root node.  The root node can be one of XML format, binary (BLOB)
    format, or text (CLOB) format.
  </api:param-description><api:param-name>root</api:param-name><api:param-type>Node</api:param-type></api:param><api:param class="javascript" name="permissions" type="Object[]" optional="true"><api:param-description>
    Security permission objects corresponding to the permissions
    for the document. If not supplied, the current user's default
    permissions are applied.  The default value used for $permissions
    can be obtained by calling xdmp.defaultPermissions(). A document that is
    created by a non-admin user (that is, by any user who does not have the
    <code xmlns="http://www.w3.org/1999/xhtml">admin</code> role) must have at least one update permission,
    otherwise the creation will throw an <code xmlns="http://www.w3.org/1999/xhtml">XDMP-MUSTHAVEUPDATE</code>
    exception.
  </api:param-description><api:param-name>permissions</api:param-name><api:param-type>Object[]</api:param-type></api:param><api:param name="collections" type="xs:string*" optional="true"><api:param-description>
    The collection URIs for collections to which this document
    belongs.  If not supplied, the document is added to the current
    user's default collections.  For each collection that is protected, the
    user must have permissions to update that collection or have the
    <code xmlns="http://www.w3.org/1999/xhtml">any-collection</code> privilege. For each unprotected collection,
    the user must have the <code xmlns="http://www.w3.org/1999/xhtml">unprotected-collections</code> privilege.
    The default value used for $collections can be obtained by calling
    <code xmlns="http://www.w3.org/1999/xhtml">xdmp:default-collections()</code>.
  </api:param-description><api:param-name>collections</api:param-name><api:param-type>String[]</api:param-type></api:param><api:param name="quality" type="xs:int?" optional="true"><api:param-description>
    The quality of this document.  A positive value increases
    the relevance score of the document in text search functions.
    The converse is true for a negative value.  The default value is 0.
  </api:param-description><api:param-name>quality</api:param-name><api:param-type>xs.int?</api:param-type></api:param><api:param name="forest-ids" type="xs:unsignedLong*" optional="true"><api:param-description>
    Specifies the ID of the forest in which this document is inserted.
    If the document already exists in the database and if $forest-ids is
    not specified, it will remain in its existing forest.  If no such
    forest exists or if no such forest is attached to the context database,
    an error is raised.  If multiple forests are specified, the document
    is inserted into one of the specifed forests.  If the document exists
    and the forest in which it is stored is set to delete-only, then you
    must set $forest-ids to include one or more forests that allow updates,
    otherwise an exception is thrown.
    <p xmlns="http://www.w3.org/1999/xhtml">
    If you have local disk failover enabled, specify the ID of the master forest.
    In the event of a failover, MarkLogic server will automatically redirect documents
    to the replica forest.  Specify the ID of the replica forest will result in a
    "forest not in database" error.
    </p>
  </api:param-description><api:param-name>forest-ids</api:param-name><api:param-type>String[]</api:param-type></api:param></api:params><api:return>null</api:return><api:privilege>
  <p xmlns="http://www.w3.org/1999/xhtml">If a new document is inserted, the <code>unprotected-uri</code> privilege
  (only if the URI is not protected), the <code>any-uri</code> privilege, or an
  appropriate URI privilege is also needed.  If adding an unprotected
  collection to a document, the <code>unprotected-collections</code> privilege
  is needed; if adding a protected collection, the user must have either
  permissions to update the collection or the
  <code>any-collection</code> privilege.</p>
</api:privilege><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
declareUpdate();
xdmp.documentInsert(
       "/example.json", {"a":"aaa"},
       [xdmp.permission("editor", "read"),
        xdmp.permission("editor", "update")],
       "http://examples.com")
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
declareUpdate();
xdmp.documentInsert(
       "/example.json",
       {"a":"aaa"},
       xdmp.defaultPermissions(),
       xdmp.defaultCollections(),
       10)
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//   Specify the forest IDs to move a delete-only document
//   from one forest to another (assuming at least one of
//   the forests allows updates).
declareUpdate();
xdmp.documentInsert(
         "/example.xml",
         {"foo":"new content here"}, null, null, 0,
         xdmp.databaseForests(xdmp.database()) )
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// create a text document 
declareUpdate();
var textNode = new NodeBuilder();
textNode.addText("This is a text document");
textNode = textNode.toNode(); 
xdmp.documentInsert("/text-doc.txt", textNode);

</pre></api:example></api:function></api:function-page>