# update\_document

The function `update_document(body, collection_name,` partial\_update, doc\_as\_upsert uploads a single document to a collection.

**Input**

* <mark style="color:purple;">body (document)</mark> – Represents the document to upload. The body must include the ID of the document to be updated, and the structure must conform to the collection schema configuration file. It must be of type **dictionary**.
* <mark style="color:purple;">collection\_name (str)</mark> – Specifies the name of the Collection into which to load the document.
* <mark style="color:purple;">partial\_update  (bool) -</mark> Specifies if to perform partial or full update. For  **partial\_update  = True**, only the fields included in body will be updated. If **partial\_update  = False**, the collection document will be replaced. Default value is False.
* <mark style="color:purple;">doc\_as\_upsert (bool) -</mark>  "Update or Insert" operation. When **upsert = True**, the upload command will attempt to run your update script. If the document exists, it will be updated. Otherwise, it will be uploaded as a new document.

**Example -**

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
new_document= {"id": "131",
               "name": "dave",
                "age": 43}

hyperspace_client.update_document(body=new_document, 
                                  collection_name='partial_update', 
                                  partial_update=True,
                                  doc_as_upsert=False);
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Document doc = new Document();
doc.setId("131");
doc.putAdditionalProperty("name", "dave");
doc.putAdditionalProperty("age", 43);

boolean partialUpdate = true;
boolean docAsUpsert = true;                        
hyperspaceClient.updateDocument(collection_name, new_document, partialUpdate, docAsUpsert);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const new_document= {"id": "131",
                        "name": "dave",
                         "age": 43};
const partialUpdate = true;
const docAsUpsert = true;

await hyperspaceClient.update(collectionName, new_document, partialUpdate, docAsUpsert);
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Response**

The following response should be received –

{'status': 'OK', 'code': 200, 'message': 'Document was successfully updated'}

{% hint style="info" %}
At the moment, updating a field of type "dense\_vector" is not possible. \
When updating a document, the "body" document must not include fields of type "dense\_vector". If the corresponding database document (meaning the document with the same ID in the database) includes such a field, it will remain unchanged.
{% endhint %}

## Conditional update

Hyperspace supports conditional update. In this case,  <mark style="color:purple;">body</mark> is provided as JS script that includes the relevant conditions.

**Example -**

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
// Not yet supported
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
JsonObject script = new JsonObject();

script =  {
          "lang": "js",
          "source": """
            if (ctx._source.tag != params.tag || ctx._source.tagType != params.tagType) {
                ctx._source.tag = params.tag;
                ctx._source.tagType = params.tagType;
            } else {
                ctx.op = "noop";
            }
          """,
          "params": {
              "tagType": "some tag type",
              "tag": "some tag",
          }
      }
      
Document doc = new Document();      
doc.setId("533");
doc.putAdditionalProperty("script", script);
client.updateDocument(collectionName, doc, true, false);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}

<pre class="language-javascript" data-line-numbers><code class="lang-javascript">const body = {
      "id": 533,
      "script": {
          "lang": "js",
          "source": """
            if (ctx._source.tag != params.tag || ctx._source.tagType != params.tagType) {
                ctx._source.tag = params.tag;
                ctx._source.tagType = params.tagType;
            } else {
                ctx.op = "noop";
            }
          """,
          "params": {
              "tagType": "some tag type",
              "tag": "some tag",
          }
      }
    };
    
<strong>await hyperspaceClient.update(collection_name=collection, body=body, partial_update=True);
</strong></code></pre>

{% endtab %}
{% endtabs %}

**Response**

In the above example, the document with id "533" will be updated as follows -&#x20;

* if params\["tag"] == document\["tag"] and params\["tagType "] == document\["tagType "], no update will be performed
* Otherwise, document will be updated as usual.
