---
title: "Put Document Command"
url: "https://docs.ravendb.net/6.2/client-api/commands/documents/put"
version: "6.2"
language: "nodejs"
---

# Put Document Command

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/commands/documents/put.md), [Java](https://docs.ravendb.net/6.2/client-api/commands/documents/put.java.md), [Python](https://docs.ravendb.net/6.2/client-api/commands/documents/put.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/commands/documents/put.php.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Use the low-level `PutDocumentCommand` to insert a new document to the database or update an existing document.
>
> - When using `PutDocumentCommand`, you must explicitly **specify the collection** to which the document will belong,
>   otherwise, the document will be placed in the `@empty` collection. See how this is done in the example below.
>
> - To insert a document to the database using a higher-level method, see [storing entities](https://docs.ravendb.net/6.2/client-api/session/storing-entities.nodejs.md).  
>   To update an existing document using a higher-level method, see [update entities](https://docs.ravendb.net/6.2/client-api/session/updating-entities.nodejs.md).
>
> - In this page:
>
>   - [Examples](#examples)
>   - [Syntax](#syntax)

## Examples

> **Note**
>
> **Put document command - using the Store's request executor**:
>
> ```javascript
> // Define the json document to 'put'
> const jsonDocument = {
>   name: "My category",
>   description: "My category description",
>   "@metadata": {
>       "@collection": "categories"
>   }
> }
>
> // Define the 'PutDocumentCommand'
> // Pass the document ID, whether to make concurrency checks, 
> // and the json document to store
> const command = new PutDocumentCommand("categories/999", null, jsonDocument);
>
> // Call 'execute' on the Store Request Executor to send the command to the server
> await documentStore.getRequestExecutor().execute(command);
>
> // Access the command result
> const result = command.result;
> const theDocumentID = result.id;
> const theDocumentCV = result.changeVector;
>
> assert.strictEqual(theDocumentID, "categories/999");
> ```

> **Note**
>
> **Put document command - using the Session's request executor**:
>
> ```javascript
> const session = documentStore.openSession();
>
> // Create a new entity
> const category = new Category();
> category.name = "My category";
> category.description = "My category description";
>
> // To be able to specify under which collection the document should be stored 
> // you need to convert the entity to a json document first.
>
> // Passing the entity as is instead of the json document
> // will result in storing the document under the "@empty" collection.
>
> const documentInfo = new DocumentInfo();
> documentInfo.collection = "categories"; // The target collection
> const jsonDocument = EntityToJson.convertEntityToJson(
>   category, documentStore.conventions, documentInfo);
>
> // Define the 'PutDocumentCommand'
> // Pass the document ID, whether to make concurrency checks,
> // and the json document to store
> const command = new PutDocumentCommand("categories/999", null, jsonDocument);
>
> // Call 'execute' on the Session Request Executor to send the command to the server
> await session.advanced.requestExecutor.execute(command);
>
> // Access the command result
> const result = command.result;
> const theDocumentID = result.id;
> const theDocumentCV = result.changeVector;
>
> assert.strictEqual(theDocumentID, "categories/999");
> ```

## Syntax

```javascript
PutDocumentCommand(id, changeVector, document);
```

| Parameter | Type | Description |
| - | - | - |
| **id** | `string` | Unique ID under which document will be stored. |
| **changeVector** | `string` | The change-vector of the document you wish to update,<br />used for [optimistic concurrency control](https://docs.ravendb.net/6.2/server/clustering/replication/change-vector.md#concurrency-control--change-vectors).<br />Pass `null` to skip the check and force the 'put'. |
| **document** | `object` | The document to store. |

```javascript
// Executing `PutDocumentCommand` returns the following object:
{
  // The document id under which the entity was stored
  id; // string
  
  // The change vector assigned to the stored document
  changeVector; // string
}
```

## See also

- [Get document](https://docs.ravendb.net/6.2/client-api/commands/documents/get.nodejs.md)
- [Delete document](https://docs.ravendb.net/6.2/client-api/commands/documents/delete.nodejs.md)
- [Send multiple commands using a batch](https://docs.ravendb.net/6.2/client-api/commands/batches/how-to-send-multiple-commands-using-a-batch.nodejs.md)
