> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lambdadb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Update data

> Update existing documents in a LambdaDB collection by ID. Modify specific fields without replacing the entire document using SDK or REST API calls.

This page shows you how to update documents in a collection.

The examples write to the default branch, `main`. The REST request accepts an optional `branch` field. To write to another branch, [create it first](https://docs.lambdadb.ai/guides/data-versioning/branches-tags-aliases.md#create-and-list-branches), then include its name in `branch`; a tag or alias is not a valid write target.

> [!NOTE]
>
> If the collection contains native embedding vector fields, update the configured source fields and LambdaDB regenerates the vector; unchanged source input reuses the existing vector. A directly supplied vector takes precedence and skips inference. See [Native embedding write behavior](https://docs.lambdadb.ai/guides/collections/native-embeddings.md#write-behavior).

```python Python theme={null}
from lambdadb import LambdaDB

with LambdaDB(
    project_api_key="YOUR_API_KEY",
    base_url="YOUR_BASE_URL",
    project_name="YOUR_PROJECT_NAME",
) as client:
    coll = client.collection("my_collection")
    docs = [
        {"id": "33201222", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... "},
        {"id": "33201223", "url": "https://en.wikipedia.org/wiki/Winamp", "title": "Winamp v2", "text": "Winamp v2 is a media player for Windows, macOS and Android, originally developed by Nullsoft."},
    ]
    coll.docs.update(docs=docs)
```

> [!NOTE]
>
> Python: `LambdaDB` supports context manager usage. `__enter__` returns the client, and `__exit__` calls `client.close()` (closing the SDK-owned HTTP client) and makes the client unusable after the `with` block. If you don't use `with`, call `client.close()` when you're done. If you pass a custom `client=`/`async_client=`, you own closing it.

```typescript TypeScript theme={null}
import { LambdaDBClient } from "@functional-systems/lambdadb";

const client = new LambdaDBClient({
  projectApiKey: "YOUR_API_KEY",
  baseUrl: "YOUR_BASE_URL",
  projectName: "YOUR_PROJECT_NAME",
});
await client.collection("my_collection").docs.update({
  docs: [
    { id: "33201222", title: "LambdaDB", text: "LambdaDB is a serverless search database ... " },
    { id: "33201223", url: "https://en.wikipedia.org/wiki/Winamp", title: "Winamp v2", text: "Winamp v2 is a media player for Windows, macOS and Android." },
  ],
});
```

```go Go theme={null}
package main

import (
  "context"
  "log"
  lambdadb "github.com/lambdadb/go-lambdadb"
)

func main() {
  ctx := context.Background()
  client := lambdadb.New(
    lambdadb.WithBaseURL("YOUR_BASE_URL"),
    lambdadb.WithProjectName("YOUR_PROJECT_NAME"),
    lambdadb.WithAPIKey("YOUR_API_KEY"),
  )
  docs := []map[string]interface{}{
    {"id": "33201222", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... "},
    {"id": "33201223", "title": "Winamp v2", "text": "Winamp v2 is a media player for Windows, macOS and Android."},
  }
  _, err := client.Collection("my_collection").Docs().Update(ctx, lambdadb.UpdateDocsInput{Docs: docs})
  if err != nil {
    log.Fatal(err)
  }
}
```

```bash cURL theme={null}
BASE_URL="YOUR_BASE_URL"
PROJECT_NAME="YOUR_PROJECT_NAME"
COLLECTION_NAME="my_collection"
LAMBDADB_PROJECT_API_KEY="YOUR_API_KEY"

curl -X POST \
  "$BASE_URL/projects/$PROJECT_NAME/collections/$COLLECTION_NAME/docs/update" \
  -H 'content-type: application/json' \
  -H "x-api-key: $LAMBDADB_PROJECT_API_KEY" \
  -d '{
    "docs": [
      {
        "id": "33201222",
        "title": "LambdaDB",
        "text": "LambdaDB is a serverless search database ... "
      },
      {
        "id": "33201223",
        "url": "https://en.wikipedia.org/wiki/Winamp",
        "title": "Winamp v2",
        "text": "Winamp v2 is a media player for Windows, macOS and Android, originally developed by Justin Frankel and Dmitry Boldyrev by their company Nullsoft, which they later sold to AOL, who sold to Radionomy in January 2014."
      }
    ]
  }'
```

> [!NOTE]
>
> Each document in a payload must contain an `id` field to uniquely identify a document to update.
> For a partitioned collection, a document in the `__default__` partition with a matching `id` value is updated if the partition field is not provided.

## How fields are updated

An update reads the existing document with strong consistency, merges the supplied fields into it, and submits the resulting document for indexing. Omitted fields remain unchanged. The resulting document must still satisfy the collection's field types and document-size limit.

| Supplied value | Behavior |
| :- | :- |
| Scalar | Replaces the existing value |
| Object | Recursively merges into an existing object, preserving omitted nested fields |
| Array | Replaces the entire array; elements are not appended or merged |
| `null` | Rejected with HTTP `400` for an indexed field. On an unindexed field, stores JSON `null`; it does not remove the field |

For example, assume `metadata` is an object field and `labels` is a keyword field. Start with this document:

```json theme={null}
{
  "id": "doc-1",
  "title": "Refund policy",
  "metadata": { "language": "en", "revision": 1 },
  "labels": ["draft", "support"]
}
```

Send this update request:

```json theme={null}
{
  "docs": [
    {
      "id": "doc-1",
      "metadata": { "revision": 2 },
      "labels": ["published"]
    }
  ]
}
```

After the update becomes visible, the document is:

```json theme={null}
{
  "id": "doc-1",
  "title": "Refund policy",
  "metadata": { "language": "en", "revision": 2 },
  "labels": ["published"]
}
```

An empty object does not clear an existing object. To remove fields, use [upsert](https://docs.lambdadb.ai/guides/documents/upsert-data.md) with the complete replacement document and omit those fields.

## Missing documents and failed requests

Update does not create documents. If any ID is missing from the selected branch and partition, the request returns HTTP `400` and none of its updates are submitted. Use upsert when a document may not exist. Updating a partitioned document requires its existing partition-field value; omitting that field selects `__default__` and does not move a document from another partition.

Input validation and native embedding generation complete before the merged documents are submitted. A failure in either stage prevents submission of the whole update request. A `202 Accepted` response confirms acceptance, not completed indexing. A timeout or transport failure after submission can still leave the write applied; [verify the resulting state before retrying](https://docs.lambdadb.ai/reference/api/introduction.md#errors-and-retries).

## Concurrent updates and cost

Update is a read-modify-write operation without a document-version precondition. Concurrent requests can read the same earlier document and submit different replacements, overwriting each other's changes even when they update different fields. Serialize writes to the same document when preserving concurrent changes matters.

The internal strongly consistent fetch is billed as read usage in addition to the update's write usage. See [Write usage](https://docs.lambdadb.ai/guides/costs/understanding-costs.md#write-usage).

## Update limits

See [request and document limits](https://docs.lambdadb.ai/guides/get-started/limits.md#requests-and-documents) for request size, document size, and ID length, and [schema limits](https://docs.lambdadb.ai/guides/get-started/limits.md#collections-and-schema) for vector dimensions.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.