> ## 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.

# Upsert data

> Insert or update documents in a LambdaDB collection using the upsert operation. Includes Python, TypeScript, Go, and cURL code examples.

This page shows you how to upsert documents into a collection.

<Note>
  If the collection contains managed embedding vector fields, send only the configured source text fields. Do not send direct vector values for managed embedding fields in an upsert request.
</Note>

<CodeGroup>
  ```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", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is an AI-native database ... ", "dense_vector": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], "sparse_vector": {"LambdaDB": 0.83, "is": 0.1, "a": 0.1, "AI": 0.7}},
          {"url": "https://en.wikipedia.org/wiki/Winamp", "title": "Winamp", "text": "Winamp is a media player for Windows, macOS and Android ...", "dense_vector": [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0], "sparse_vector": {"0": 0.8, "4": 0.1, "11": 0.1, "63": 0.54}},
      ]
      coll.docs.upsert(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.
  </Note>

  ```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",
  });
  const collection = client.collection("my_collection");
  await collection.docs.upsert({
    docs: [
      { id: "33201222", url: "https://en.wikipedia.org/wiki/LambdaDB", title: "LambdaDB", text: "LambdaDB is an AI-native database ... ", denseVector: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], sparseVector: { LambdaDB: 0.83, is: 0.1, a: 0.1, AI: 0.7 } },
      { url: "https://en.wikipedia.org/wiki/Winamp", title: "Winamp", text: "Winamp is a media player for Windows, macOS and Android ...", denseVector: [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0], sparseVector: { "0": 0.8, "4": 0.1, "11": 0.1, "63": 0.54 } },
    ],
  });
  ```

  ```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"),
    )
    coll := client.Collection("my_collection")
    myDocs := []map[string]interface{}{
      {"id": "33201222", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is an AI-native database ... ", "dense_vector": []float64{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0}, "sparse_vector": map[string]float64{"LambdaDB": 0.83, "is": 0.1, "a": 0.1, "AI": 0.7}},
      {"url": "https://en.wikipedia.org/wiki/Winamp", "title": "Winamp", "text": "Winamp is a media player for Windows, macOS and Android ...", "dense_vector": []float64{1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0}},
    }
    _, err := coll.Docs().Upsert(ctx, lambdadb.UpsertDocsInput{Docs: myDocs})
    if err != nil {
      log.Fatal(err)
    }
  }
  ```

  ```bash cURL theme={null}
  LAMBDADB_PROJECT_API_KEY="YOUR_API_KEY"

  curl -X POST \
    "$BASE_URL/projects/$PROJECT_NAME/collections/{collectionName}/docs/upsert" \
    -H 'content-type: application/json' \
    -H 'x-api-key: $LAMBDADB_PROJECT_API_KEY' \
    -d '{
      "docs": [
        {
          "id": "33201222",
          "url": "https://en.wikipedia.org/wiki/LambdaDB",
          "title": "LambdaDB",
          "text": "LambdaDB is an AI-native database ... ",
          "dense_vector" : [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
          "sparse_vector" : {"LambdaDB": 0.83, "is": 0.1, "a": 0.1, "AI": 0.7}
        },
        {
          "url": "https://en.wikipedia.org/wiki/Winamp",
          "title": "Winamp",
          "text": "Winamp is a media player for Windows, macOS and Android ...",
          "dense_vector" : [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],
          "sparse_vector" : {"0": 0.8, "4": 0.1, "11": 0.1, "63": 0.54}
        }
      ]
    }'
  ```
</CodeGroup>

## Response

```json theme={null}
{
  "message": "Upsert request is accepted"
}
```

Each document implicitly contains an `id` field in order to uniquely identify a document.
A unique string value is auto-generated by the system if `id` field is not provided in an upsert request.
If you want to overwrite the entire document, you can do so by providing the `id` field in the document.

<Info>
  For a partitioned collection, documents are upserted into the `__default__` partition if the partition field is not provided.
</Info>

## Upsert limits

| Metric                       | Limit          |
| :--------------------------- | :------------- |
| Max payload size             | 6MB            |
| Max length for a document ID | 512 characters |
| Max vector dimensions        | 4,096          |
| Max document size            | 5MB            |

When upserting larger amounts of data, it is recommended to use
[bulk-upsert](/guides/documents/bulk-upsert-data) operation.
