Skip to content
Docs

MongoDB Atlas Vector Store Provider

The MongoDB Atlas provider implements the vectorstore.VectorStore interface using MongoDB Atlas Vector Search via the HTTP Data API. It supports cosine similarity search with metadata filtering through the $vectorSearch aggregation stage.

Choose MongoDB Atlas when your application already uses MongoDB and you want to add vector search alongside your existing document data. Atlas Vector Search integrates with MongoDB’s aggregation pipeline, allowing you to combine vector similarity with traditional document queries without managing a separate vector database.

Terminal window
go get github.com/lookatitude/beluga-ai/rag/vectorstore/providers/mongodb
package main
import (
"context"
"log"
"os"
"github.com/lookatitude/beluga-ai/config"
"github.com/lookatitude/beluga-ai/rag/vectorstore"
"github.com/lookatitude/beluga-ai/schema"
_ "github.com/lookatitude/beluga-ai/rag/vectorstore/providers/mongodb"
)
func main() {
store, err := vectorstore.New("mongodb", config.ProviderConfig{
BaseURL: os.Getenv("MONGODB_DATA_API_URL"),
APIKey: os.Getenv("MONGODB_API_KEY"),
Options: map[string]any{
"database": "my_db",
"collection": "documents",
"index": "vector_index",
},
})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
docs := []schema.Document{
{ID: "doc1", Content: "Go is a statically typed language", Metadata: map[string]any{"lang": "en"}},
}
embeddings := [][]float32{make([]float32, 1536)}
err = store.Add(ctx, docs, embeddings)
if err != nil {
log.Fatal(err)
}
}
ParameterTypeDefaultDescription
BaseURLstring(required)MongoDB Data API endpoint
APIKeystring(optional)Data API key
Options["database"]stringbelugaDatabase name
Options["collection"]stringdocumentsCollection name
Options["index"]stringvector_indexAtlas Vector Search index name
  1. Create a MongoDB Atlas cluster
  2. Enable the Data API for your cluster
  3. Create an Atlas Vector Search index on the embedding field:
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 1536,
"similarity": "cosine"
}
]
}
import (
mongostore "github.com/lookatitude/beluga-ai/rag/vectorstore/providers/mongodb"
)
store := mongostore.New(
os.Getenv("MONGODB_DATA_API_URL"),
mongostore.WithDatabase("production"),
mongostore.WithCollection("docs"),
mongostore.WithIndex("prod_vector_index"),
mongostore.WithAPIKey(os.Getenv("MONGODB_API_KEY")),
)

Metadata filters are applied to the metadata field path:

results, err := store.Search(ctx, queryVec, 10,
vectorstore.WithFilter(map[string]any{
"lang": "en",
}),
)

This translates to a MongoDB filter on metadata.lang.

results, err := store.Search(ctx, queryVec, 10,
vectorstore.WithThreshold(0.7),
)
for _, doc := range results {
fmt.Printf("ID: %s, Score: %.4f, Content: %s\n", doc.ID, doc.Score, doc.Content)
}

Documents are stored with the following structure:

{
"_id": "doc1",
"content": "Document text content",
"embedding": [0.1, 0.2, ...],
"metadata": {
"lang": "en",
"category": "technical"
}
}
store := mongostore.New(
os.Getenv("MONGODB_DATA_API_URL"),
mongostore.WithHTTPClient(customClient),
mongostore.WithDatabase("my_db"),
)