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

# Updating Documents

> Update metadata and text in VectoriaDB

Learn how to update documents in your VectoriaDB index.

## Update Metadata Only

Metadata-only updates are instant and don't trigger re-embedding:

```ts title="src/update-metadata.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
await toolIndex.updateMetadata('users:list', {
  id: 'users:list',
  toolName: 'list',
  owner: 'users',
  tags: ['read', 'user-management', 'legacy'],
  risk: 'safe',
  deprecated: true,
});
```

<Tip>
  Use `updateMetadata` when only changing metadata fields. It's significantly faster than a full update since no embedding regeneration is required.
</Tip>

## Update Text and Metadata

When text changes, VectoriaDB re-embeds the document:

```ts title="src/update-document.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
await toolIndex.update('users:list', {
  text: 'Updated description for user listing',
  metadata: {
    id: 'users:list',
    toolName: 'list',
    owner: 'users',
    tags: ['read'],
    risk: 'safe',
  },
});
```

### Smart Re-embedding

The `update` method only re-embeds when necessary:

```ts title="src/smart-update.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Only updates metadata (no re-embed)
await db.update('id', {
  metadata: newMetadata,
});

// Re-embeds because text changed
await db.update('id', {
  text: 'New text content',
  metadata: newMetadata,
});

// Force re-embed even if text unchanged
await db.update('id', {
  text: existingText,
  metadata: newMetadata,
}, { forceReembed: true });
```

## Batch Updates

```ts title="src/batch-update.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const result = await toolIndex.updateMany([
  {
    id: 'users:list',
    text: 'New description',
    metadata: { /* ... */ },
  },
  {
    id: 'billing:charge',
    metadata: { deprecated: true }, // Metadata-only update
  },
]);

console.log(`Updated: ${result.updated}, Re-embedded: ${result.reembedded}`);
```

### Return Value

`updateMany` returns statistics:

* `updated`: Total documents updated
* `reembedded`: Documents that required re-embedding

## Upsert Pattern

VectoriaDB doesn't have a built-in upsert. Use this pattern:

```ts title="src/upsert.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
async function upsertDocument(id: string, text: string, metadata: T) {
  if (db.has(id)) {
    await db.update(id, { text, metadata });
  } else {
    await db.add(id, text, metadata);
  }
}
```

## Partial Metadata Updates

To update only some metadata fields:

```ts title="src/partial-update.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Get existing document
const existing = db.get('users:list');
if (!existing) throw new Error('Not found');

// Merge updates
await db.updateMetadata('users:list', {
  ...existing.metadata,
  deprecated: true,
  tags: [...existing.metadata.tags, 'legacy'],
});
```

## Error Handling

```ts title="src/update-errors.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
import { DocumentNotFoundError, DocumentValidationError } from 'vectoriadb';

try {
  await db.update(id, updates);
} catch (error) {
  if (error instanceof DocumentNotFoundError) {
    console.log(`Document ${error.documentId} not found`);
  } else if (error instanceof DocumentValidationError) {
    console.log(`Validation failed: ${error.message}`);
  }
}
```

## Related

<CardGroup cols={3}>
  <Card title="Adding Documents" icon="plus" href="/vectoriadb/guides/core/adding-documents">
    Add new documents
  </Card>

  <Card title="Removing Documents" icon="trash" href="/vectoriadb/guides/core/removing-documents">
    Remove documents
  </Card>

  <Card title="Search" icon="magnifying-glass" href="/vectoriadb/guides/search/basic-search">
    Query updated documents
  </Card>
</CardGroup>
