---
title: "Database Vector Stores"
description: "Database Vector Stores provide persistent indexing, metadata filtering, and approximate nearest neighbor (ANN) retrieval capabilities across relational databases, distributed search engines, in-mem..."
last_updated: "2026-10-11T17:37:10.218815+00:00"
canonical_url: "https://www.doc0.dev/docs/faa36707-7c28-4f69-a18f-700ff61c704e/technical/vector-stores/database-vector-stores"
---

<details>
<summary>Relevant source files</summary>

The following files were used as context for generating this wiki page:

- [mem0/vector_stores/oracledb.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/oracledb.py)
- [mem0-ts/src/oss/src/vector_stores/oracledb.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/oracledb.ts)
- [mem0/vector_stores/valkey.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/valkey.py)
- [mem0-ts/src/oss/src/vector_stores/baidu.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/baidu.ts)
- [mem0-ts/src/oss/src/vector_stores/neptune_analytics.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/neptune_analytics.ts)
- [mem0/vector_stores/redis.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/redis.py)
- [mem0/vector_stores/neptune_analytics.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/neptune_analytics.py)
- [mem0-ts/src/oss/src/vector_stores/cassandra.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/cassandra.ts)
- [mem0-ts/src/oss/src/vector_stores/redis.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/redis.ts)
- [mem0/vector_stores/opensearch.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/opensearch.py)
- [mem0-ts/src/oss/src/vector_stores/valkey.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/valkey.ts)
- [mem0/vector_stores/baidu.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/baidu.py)
- [mem0-ts/src/oss/src/vector_stores/opensearch.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/opensearch.ts)
- [mem0/vector_stores/mongodb.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/mongodb.py)
- [mem0-ts/src/oss/src/vector_stores/elasticsearch.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/elasticsearch.ts)
- [mem0-ts/src/oss/src/vector_stores/azure_mysql.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/azure_mysql.ts)
- [mem0/configs/vector_stores/oracledb.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/configs/vector_stores/oracledb.py)
- [mem0/configs/vector_stores/__init__.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/configs/vector_stores/__init__.py)
- [mem0/vector_stores/chroma.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/chroma.py)
- [mem0/vector_stores/__init__.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/__init__.py)
- [mem0-ts/src/oss/src/vector_stores/mongodb.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/mongodb.ts)
- [mem0/vector_stores/azure_ai_search.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/azure_ai_search.py)
- [mem0-ts/src/oss/src/vector_stores/weaviate.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/weaviate.ts)
- [mem0/vector_stores/faiss.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/faiss.py)
- [mem0/vector_stores/weaviate.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/weaviate.py)
- [mem0/vector_stores/cassandra.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/cassandra.py)
- [mem0/vector_stores/milvus.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/milvus.py)
- [mem0-ts/src/oss/src/vector_stores/vectorize.ts](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/vectorize.ts)
- [mem0-ts/src/oss/src/vector_stores/pgvector.ts](https://github.com/blade47/mem0-ts/src/oss/src/vector_stores/pgvector.ts)
- [mem0/vector_stores/elasticsearch.py](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/elasticsearch.py)
</details>

## Overview

Database Vector Stores provide persistent indexing, metadata filtering, and approximate nearest neighbor (ANN) retrieval capabilities across relational databases, distributed search engines, in-memory data structures, and graph analytics systems. Within Mem0, these database backends handle the storage and querying of long-term memory embeddings alongside associated payloads, user tenant contexts, and timestamps. Unlike standalone [Embedded Vector Stores](https://www.doc0.dev/docs/faa36707-7c28-4f69-a18f-700ff61c704e/technical/vector-stores/embedded-vector-stores) or managed [Cloud Vector Backends](https://www.doc0.dev/docs/faa36707-7c28-4f69-a18f-700ff61c704e/technical/vector-stores/cloud-vector-backends), database vector stores integrate embedding storage directly into existing enterprise data systems like PostgreSQL via `pgvector`, Elasticsearch, OpenSearch, Redis, Valkey, Apache Cassandra, Amazon Neptune Analytics, Oracle AI Vector Search, MongoDB, Azure AI Search, and Baidu Mochow.

Sources: [mem0-ts/src/oss/src/vector_stores/pgvector.ts:217-245](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L217-L245)

The primary design challenge solved by these drivers is bridging multi-dimensional vector math with structured relational tables, document schemas, or inverted indices. Because different engines handle vector indexing via distinct internal mechanisms—such as PostgreSQL native `vector` data types, Elasticsearch `dense_vector` fields with HNSW graphs, or Redis Hash keys with RediSearch modules—each backend abstracts the connection lifecycle, schema initialization, parameter binding, and query filtering into a unified `VectorStore` interface.

Sources: [mem0-ts/src/oss/src/vector_stores/pgvector.ts:217-245](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L217-L245)

```mermaid
flowchart TD
    A["Memory Core Engine"] --> B["VectorStore Interface"]
    B --> C["Relational DBs<br>(pgvector, Azure MySQL, Oracle)"]
    B --> D["Search Engines<br>(Elasticsearch, OpenSearch, Azure AI Search)"]
    B --> E["In-Memory & KV<br>(Redis, Valkey)"]
    B --> F["Distributed & Graph<br>(Cassandra, Neptune Analytics, Milvus, Baidu)"]
```

Sources: [mem0-ts/src/oss/src/vector_stores/pgvector.ts:217-245](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L217-L245)

## Relational Database Integrations: pgvector, Azure MySQL, and Oracle DB

Relational database integrations extend traditional SQL tables with native or JSON-serialized vector storage and similarity operators. 

In PGVector, tables are initialized with a `UUID` primary key, a `vector` data type column sized to `embeddingModelDims`, and a `JSONB` payload column. During nearest neighbor search, `pgvector` executes queries using the `<->` cosine distance operator, sorting results and bounding output sizes with `LIMIT $2`. Parameterized filters are dynamically constructed via `buildFilterConditions()`, translating metadata JSON path lookups into SQL expressions with strict parameter indexing to prevent injection.

Sources: [mem0-ts/src/oss/src/vector_stores/pgvector.ts:284-446](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L284-L446)

For Azure MySQL, embeddings and JSON payloads are stored as JSON columns and evaluated via application-level `cosineSimilarity()` loops over fetched rows, since MySQL handles vector distance sorting in memory after fetching candidate sets.

Sources: [mem0-ts/src/oss/src/vector_stores/azure_mysql.ts:123-211](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/azure_mysql.ts#L123-L211)

Oracle AI Vector Search utilizes Oracle 23.4+ native `VECTOR` data types and `JSON` payload columns inside relational tables. Search operations invoke `VECTOR_DISTANCE(vector, :query_vec, ...)` combined with `ORDER BY distance FETCH APPROX FIRST :max_rows ROWS ONLY`. When executing filtered searches, Oracle conditionally omits index hints or injects `VECTOR_INDEX_TRANSFORM` depending on filter presence.

Sources: [mem0/vector_stores/oracledb.py:350-402](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/oracledb.py#L350-L402)

```mermaid
sequenceDiagram
    participant App as Memory Client
    participant PG as PGVector Store
    participant DB as PostgreSQL Engine
    App->>PG: search(queryVector, topK, filters)
    PG->>DB: SELECT id, vector <=> $1::vector, payload WHERE ... ORDER BY distance LIMIT $2
    DB-->>PG: Result rows with distance and JSONB payload
    PG-->>App: Normalized VectorStoreResult[] array
```

Sources: [mem0-ts/src/oss/src/vector_stores/pgvector.ts:414-446](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L414-L446)

| Provider | Vector Storage Format | Index / Distance Mechanism | Metadata / Filter Strategy |
| :--- | :--- | :--- | :--- |
| **pgvector** | `vector(dims)` column | HNSW / DiskANN via `<->` operator | JSONB operators (`payload->>'key'`) |
| **Azure MySQL** | `JSON` column | Application-level `cosineSimilarity()` | `JSON_EXTRACT(payload, ?)` |
| **Oracle DB** | `VECTOR(dims)` column | HNSW / IVF neighbor graph via `VECTOR_DISTANCE` | Oracle JSON relational duality / bind vars |

Sources: [mem0-ts/src/oss/src/vector_stores/pgvector.ts:315-349](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L315-L349), [mem0-ts/src/oss/src/vector_stores/azure_mysql.ts:124-189](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/azure_mysql.ts#L124-L189), [mem0/vector_stores/oracledb.py:321-364](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/oracledb.py#L321-L364)

## Search Engines: Elasticsearch, OpenSearch, and Azure AI Search

Distributed search and retrieval engines leverage inverted indices alongside vector quantization and HNSW graphs to execute hybrid keyword and semantic searches.

Sources: [mem0/vector_stores/opensearch.py:83-135](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/opensearch.py#L83-L135)

Elasticsearch and OpenSearch store embeddings in `dense_vector` or `knn_vector` fields configured with `nmslib` or native engines using cosine similarity. When inserting documents, payloads are flattened into `metadata` objects. During vector search, queries construct a `knn` block specifying `query_vector`, `k`, and `num_candidates` (typically `topK * 2`). Metadata filters are validated against a strict regex (`/^[a-zA-Z_][a-zA-Z0-9_]*$/`) and injected into the KNN filter block as boolean term queries.

Sources: [mem0-ts/src/oss/src/vector_stores/elasticsearch.ts:134-164](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/elasticsearch.ts#L134-L164), [mem0-ts/src/oss/src/vector_stores/opensearch.ts:160-194](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/opensearch.ts#L160-L194)

Azure AI Search configures indices with `SearchField` vector definitions (supporting `Collection(Edm.Single)` or `Collection(Edm.Half)` precision) and optional Scalar or Binary Quantization compression profiles (`ScalarQuantizationCompression`, `BinaryQuantizationCompression`). Filtering is performed via OData filter expressions generated by `_build_filter_expression()`.

Sources: [mem0/vector_stores/azure_ai_search.py:105-163](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/azure_ai_search.py#L105-L163)

> [!NOTE]
> OpenSearch and Elasticsearch require explicit index settings (`index.knn: true` in OpenSearch; `dense_vector` mapping with `index: true` in Elasticsearch) to enable approximate nearest neighbor indexing. Without these settings, searches fall back to exact brute-force scans across all documents.

Sources: [mem0/vector_stores/elasticsearch.py:72-95](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/elasticsearch.py#L72-L95)

| Search Backend | Vector Mapping Type | Default Distance / Metric | Filter Execution Mechanism |
| :--- | :--- | :--- | :--- |
| **Elasticsearch** | `dense_vector` | `cosine` | `knn.filter` boolean term clauses |
| **OpenSearch** | `knn_vector` | `cosinesimil` | QueryDSL bool / term filters on `payload` |
| **Azure AI Search** | `Collection(Edm.Single)` | Cosine via HNSW profile | OData filter expression (`eq`, `and`) |

Sources: [mem0/vector_stores/elasticsearch.py:72-95](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/elasticsearch.py#L72-L95), [mem0/vector_stores/opensearch.py:83-107](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/opensearch.py#L83-L107), [mem0/vector_stores/azure_ai_search.py:135-160](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/azure_ai_search.py#L135-L160)

## In-Memory & Key-Value Stores: Redis and Valkey

In-memory data stores like Redis and Valkey employ RediSearch and RedisVL modules to index hash structures containing binary-encoded floating-point vector embeddings.

Sources: [mem0/vector_stores/redis.py:48-78](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/redis.py#L48-L78)

The `RedisDB` and `ValkeyDB` drivers store entries as Redis Hashes using keys prefixed with `mem0:{collection_name}:{id}`. Float32 arrays are converted to raw byte buffers (`np.array(vector, dtype=np.float32).tobytes()` in Python or `Buffer.from(new Float32Array(query).buffer)` in TypeScript) before persistence.

Sources: [mem0/vector_stores/redis.py:123-147](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/redis.py#L123-L147), [mem0-ts/src/oss/src/vector_stores/valkey.ts:432-439](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/valkey.ts#L432-L439)

Schema definitions declare fields such as `memory_id`, `hash`, `agent_id`, `run_id`, `user_id` as `TAG` types, `memory` and `metadata` as `TEXT` or `TAG`, `created_at` as `NUMERIC`, and `embedding` as a `VECTOR` attribute using FLAT or HNSW algorithms.

Sources: [mem0/vector_stores/valkey.py:18-34](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/valkey.py#L18-L34), [mem0/vector_stores/redis.py:20-36](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/redis.py#L20-L36)

```typescript
// Example: Building a Valkey / Redis pre-filter expression
export function buildRedisFilterExpr(filters?: SearchFilters): string {
  if (!filters) return "*";
  const conditions = Object.entries(toSnakeCase(filters))
    .filter(([, value]) => value !== null && value !== undefined)
    .map(([key, value]) => `@${key}:{${escapeRedisTagValue(value)}}`);
  return conditions.length > 0 ? conditions.join(" ") : "*";
}
```
Sources: [mem0-ts/src/oss/src/vector_stores/redis.ts:141-147](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/redis.ts#L141-L147)

> [!CAUTION]
> Tag filter values in RediSearch must be escaped via `escapeTagValue()` or `escapeRedisTagValue()` to neutralize special characters such as `*`, `|`, and whitespace. Unescaped tag values alter query syntax and can allow tenant-isolation filter bypasses.

Sources: [mem0/vector_stores/valkey.py:48-57](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/valkey.py#L48-L57), [mem0-ts/src/oss/src/vector_stores/valkey.ts:22-27](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/valkey.ts#L22-L27)

## Distributed & Graph Backends: Cassandra, Neptune Analytics, Milvus, and Baidu Mochow

Distributed and specialized graph stores handle massive memory throughput using custom partition keys and vector-optimized storage engines.

Sources: [mem0-ts/src/oss/src/vector_stores/cassandra.ts:41-80](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/cassandra.ts#L41-L80)

- **Apache Cassandra**: Stores vectors as `list<float>` and payloads as JSON text inside tables structured under configurable keyspaces (default `mem0`). Search queries scan rows in pages (`PAGE_SIZE = 500`), normalize vectors, apply in-memory metadata filters, and maintain top-K results via `pushTopResult()`.
- **Amazon Neptune Analytics**: Operates as a serverless graph database where memory entities are stored as graph vertices with label `MEM0_VECTOR_{collection_name}` and identifier `~id`. Vector upserts and similarity searches execute Cypher procedures such as `CALL neptune.algo.vectors.upsert(n, embedding)` and `CALL neptune.algo.vectors.topKByEmbeddingWithFiltering(...)`.
- **Milvus / Zilliz**: Configures collections with `AUTOINDEX` for float vectors and `SPARSE_INVERTED_INDEX` with built-in `BM25` functions for hybrid sparse-dense retrieval over auto-tokenized `text` fields.
- **Baidu Mochow**: Utilizes typed tables (`STRING` id, `FLOAT_VECTOR` vector, `JSON` metadata) with HNSW vector indices and `BM25SearchRequest` full-text capabilities.

Sources: [mem0-ts/src/oss/src/vector_stores/cassandra.ts:143-185](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/cassandra.ts#L143-L185), [mem0/vector_stores/neptune_analytics.py:41-61](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/neptune_analytics.py#L41-L61), [mem0/vector_stores/milvus.py:33-65](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/milvus.py#L33-L65), [mem0-ts/src/oss/src/vector_stores/baidu.ts:352-391](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/baidu.ts#L352-L391)

```mermaid
graph LR
    subgraph Cassandra
        C1["Scan Rows (Paged)"] --> C2["Normalize Vector & Parse Payload"]
        C2 --> C3["Filter & pushTopResult()"]
    end
    subgraph Neptune
        N1["Cypher Query Generation"] --> N2["neptune.algo.vectors.topKByEmbeddingWithFiltering"]
        N2 --> N3["Parse Node & Score Output"]
    end
    subgraph Milvus
        M1["Dense Vectors (AUTOINDEX)"] --> M2["Sparse Vectors (BM25 Function)"]
        M2 --> M3["Hybrid Search Result Ranking"]
    end
```

Sources: [mem0-ts/src/oss/src/vector_stores/cassandra.ts:143-185](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/cassandra.ts#L143-L185), [mem0/vector_stores/neptune_analytics.py:159-198](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/neptune_analytics.py#L159-L198), [mem0/vector_stores/milvus.py:66-125](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/milvus.py#L66-L125)

```typescript
async search(
  query: number[],
  topK: number = 5,
  filters?: SearchFilters,
): Promise<VectorStoreResult[]> {
  await this.initialize();
  this.assertVectorDimension(query, "Query");

  const scored: VectorStoreResult[] = [];
  await this.scanRows(
    `
      SELECT id, vector, payload
      FROM ${this.keyspace}.${this.collectionName}
    `,
    async (row) => {
      const vector = this.normalizeVector(row.vector);
      const payload = this.parsePayload(row.payload);
      if (!vector || vector.length !== this.dimension) {
        return;
      }
      const item: CassandraVector = {
        id: String(row.id),
        vector,
        payload,
      };
      if (!this.filterVector(item, filters)) {
        return;
      }
      this.pushTopResult(
        scored,
        {
          id: item.id,
          payload: item.payload,
          score: this.cosineSimilarity(query, item.vector),
        },
        topK,
      );
    },
    Math.max(topK, CassandraDB.PAGE_SIZE),
  );

  return scored;
}
```
Sources: [mem0-ts/src/oss/src/vector_stores/cassandra.ts:143-185](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/cassandra.ts#L143-L185)

## Configuration, Connection Management, and Lazy Loading

Database vector store initializers implement lazy connection loading to prevent optional peer dependencies (such as `@elastic/elasticsearch`, `pymongo`, `cassandra-driver`, `redis`, or `oracledb`) from breaking base package imports when a provider is not actively in use.

Sources: [mem0-ts/src/oss/src/vector_stores/elasticsearch.ts:63-65](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/elasticsearch.ts#L63-L65), [mem0-ts/src/oss/src/vector_stores/mongodb.ts:35-37](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/mongodb.ts#L35-L37)

Clients and connection pools are initialized on demand during the first call to `initialize()`, which caches its execution promise (`_initPromise`) to prevent race conditions during concurrent startup sequences.

Sources: [mem0-ts/src/oss/src/vector_stores/opensearch.ts:120-126](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/opensearch.ts#L120-L126)

```typescript
// Example: Lazy peer loading pattern in OpenSearch and Elasticsearch
async initialize(): Promise<void> {
  if (!this._initPromise) {
    this._initPromise = this._doInitialize();
  }
  return this._initPromise;
}
```
Sources: [mem0-ts/src/oss/src/vector_stores/opensearch.ts:120-126](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/opensearch.ts#L120-L126)

| Configuration Property | Target Stores | Purpose & Default Behavior |
| :--- | :--- | :--- |
| `collection_name` / `tableName` | All Stores | Namespace or table identifier (defaults to `mem0` or `memories`) |
| `embeddingModelDims` / `dimension` | All Stores | Vector dimensionality (defaults to `1536`) |
| `autoRefresh` | OpenSearch | Controls `indices.refresh()` post-insert (default `false` for Serverless compatibility) |
| `useAzureCredential` | Azure MySQL, Azure AI Search | Authenticates via `@azure/identity` DefaultAzureCredential |

Sources: [mem0/configs/vector_stores/oracledb.py:41-69](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/configs/vector_stores/oracledb.py#L41-L69), [mem0/vector_stores/opensearch.py:62-81](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/opensearch.py#L62-L81), [mem0-ts/src/oss/src/vector_stores/azure_mysql.ts:34-46](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/azure_mysql.ts#L34-L46)

## Error Handling, Sanitization, and Security Invariants

Database vector stores enforce strict input sanitization and parameter binding guards to protect against injection attacks and malformed embeddings.

Sources: [mem0/vector_stores/mongodb.py:150-165](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/mongodb.py#L150-L165)

- **Filter Key Validation**: Drivers such as Elasticsearch, OpenSearch, pgvector, and Baidu enforce strict regular expression validation on filter keys (`/^[a-zA-Z_][a-zA-Z0-9_]*$/`) to prevent arbitrary query injection through metadata keys.
- **Operator Blocking**: MongoDB drivers reject filter values that contain dictionaries (`$ne`, `$gt`, etc.) to prevent NoSQL query operator injection.
- **Dimension Checking**: Insert and search operations validate that input vector lengths match configured embedding dimensions before issuing database commands.

Sources: [mem0/vector_stores/mongodb.py:150-165](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/mongodb.py#L150-L165), [mem0-ts/src/oss/src/vector_stores/elasticsearch.ts:26-44](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0-ts/src/oss/src/vector_stores/elasticsearch.ts#L26-L44), [mem0/vector_stores/opensearch.py:25-53](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/opensearch.py#L25-L53)

> [!WARNING]
> Bypassing filter key validation or passing unvalidated user input directly into MongoDB query objects or SQL string templates can permit query manipulation. Always use the provided validation helpers (`_validate_filter`, `validateFilterValue`) prior to query construction.

Sources: [mem0/vector_stores/mongodb.py:150-165](https://github.com/blade47/mem0/blob/b7ad69afda6b6ed030347c66d48a13e4de9dec08/mem0/vector_stores/mongodb.py#L150-L165)

## Related

- [Vector Store Architecture](https://www.doc0.dev/docs/faa36707-7c28-4f69-a18f-700ff61c704e/technical/vector-stores/vector-store-architecture)
- [Cloud Vector Backends](https://www.doc0.dev/docs/faa36707-7c28-4f69-a18f-700ff61c704e/technical/vector-stores/cloud-vector-backends)


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/faa36707-7c28-4f69-a18f-700ff61c704e/llms.txt) for all pages in this wiki.
