Skip to main content
Learn how to update documents in your VectoriaDB index.

Update Metadata Only

Metadata-only updates are instant and don’t trigger re-embedding:
src/update-metadata.ts
await toolIndex.updateMetadata('users:list', {
  id: 'users:list',
  toolName: 'list',
  owner: 'users',
  tags: ['read', 'user-management', 'legacy'],
  risk: 'safe',
  deprecated: true,
});
Use updateMetadata when only changing metadata fields. It’s significantly faster than a full update since no embedding regeneration is required.

Update Text and Metadata

When text changes, VectoriaDB re-embeds the document:
src/update-document.ts
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:
src/smart-update.ts
// 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

src/batch-update.ts
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:
src/upsert.ts
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:
src/partial-update.ts
// 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

src/update-errors.ts
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}`);
  }
}

Adding Documents

Add new documents

Removing Documents

Remove documents

Search

Query updated documents