Skip to content

MongoVectorRepository

Defined in: mongo-vector-repository.ts:26

A MongoRepository<T> that also implements Mantle’s VectorRepository<T> via MongoDB Atlas Vector Search ($vectorSearch aggregation stage).

Requirements — this is an Atlas feature, not core MongoDB:

  • An Atlas cluster (M0 free tier works) running MongoDB 6.0.11+/7.0.2+.
  • An Atlas Vector Search index named vectorIndexName on this collection, covering vectorField as type: "vector". Fields referenced in a findSimilar where clause must be indexed as type: "filter" in the same index.

Every findSimilar result carries the Atlas similarity score as _score — HIGHER is more similar (score ∈ [0, 1] for cosine/dotProduct). The embedding field itself is stripped from results.

Register it with VectorRepositoryService to expose POST /<path>/similar exactly like the Pinecone/Qdrant adapters.

T extends Record<string, unknown>

D = Partial<T>

new MongoVectorRepository<T, D>(app): MongoVectorRepository<T, D>

Defined in: mongo-repository.ts:44

MantleApplication

MongoVectorRepository<T, D>

MongoRepository.constructor

protected optional _session?: ClientSession

Defined in: mongo-repository.ts:42

Session bound by withTransaction() — every driver call passes it when set.

MongoRepository._session


readonly candidateMultiplier: number = 10

Defined in: mongo-vector-repository.ts:41

ANN candidate pool multiplier: numCandidates = topK * candidateMultiplier (capped at Atlas’s 10000 limit). Atlas recommends 10–20× for good recall.

10

protected readonly client: MongoClient

Defined in: mongo-repository.ts:22

MongoRepository.client


abstract readonly collectionName: string

Defined in: mongo-repository.ts:26

The MongoDB collection name to target.

MongoRepository.collectionName


readonly createdAtField: string = "createdAt"

Defined in: mongo-repository.ts:31

Field written for auto-managed creation timestamps.

"createdAt"

MongoRepository.createdAtField


protected readonly db: Db

Defined in: mongo-repository.ts:23

MongoRepository.db


readonly fieldMap: Record<string, string> = {}

Defined in: mongo-repository.ts:39

Entity-field-to-document-field overrides, for a collection whose field names don’t match the entity (e.g. a brownfield collection using snake_case). Does not apply to id — Mongo’s id is always the driver’s _id.

{}

MongoRepository.fieldMap


readonly timestamps: boolean = true

Defined in: mongo-repository.ts:29

When true, writes maintain createdAt/updatedAt as BSON Date fields.

true

MongoRepository.timestamps


readonly updatedAtField: string = "updatedAt"

Defined in: mongo-repository.ts:33

Field written for auto-managed update timestamps.

"updatedAt"

MongoRepository.updatedAtField


readonly vectorField: string = "embedding"

Defined in: mongo-vector-repository.ts:34

Document field storing the embedding.

"embedding"

readonly vectorIndexName: string = "vector_index"

Defined in: mongo-vector-repository.ts:31

Name of the Atlas Vector Search index on this collection.

"vector_index"

get protected collection(): Collection<Document>

Defined in: mongo-repository.ts:50

Escape hatch — the underlying driver Collection for native queries ($regex, aggregations, …).

Collection<Document>

MongoRepository.collection

count(params?): Promise<number>

Defined in: mongo-repository.ts:172

QueryParams

Promise<number>

VectorRepository.count

MongoRepository.count


deleteById(id): Promise<T>

Defined in: mongo-repository.ts:161

Id

Promise<T>

VectorRepository.deleteById

MongoRepository.deleteById


deleteVector(id): Promise<T>

Defined in: mongo-vector-repository.ts:102

Delete a vector and its associated record

Id

Promise<T>

VectorRepository.deleteVector


describe(): RepositoryCapabilities

Defined in: mongo-repository.ts:54

Optional capability introspection. All

RepositoryCapabilities

adapters implement it; user repositories may omit it.

VectorRepository.describe

MongoRepository.describe


findAll(params?): Promise<T[]>

Defined in: mongo-repository.ts:66

QueryParams

Promise<T[]>

VectorRepository.findAll

MongoRepository.findAll


findById(id): Promise<T | null>

Defined in: mongo-repository.ts:83

Id

Promise<T | null>

VectorRepository.findById

MongoRepository.findById


findSimilar(vector, topK, params?): Promise<T & object[]>

Defined in: mongo-vector-repository.ts:46

Top-K nearest neighbours via $vectorSearch. HIGHER _score is more similar.

number[]

number

QueryParams

Promise<T & object[]>

VectorRepository.findSimilar


protected fromDocument(doc): T

Defined in: mongo-repository.ts:238

Map a driver document to the entity shape: _id: ObjectId becomes id: string.

Record<string, unknown>

T

MongoRepository.fromDocument


patchById(id, data): Promise<T>

Defined in: mongo-repository.ts:129

Id

D

Promise<T>

VectorRepository.patchById

MongoRepository.patchById


save(data): Promise<T>

Defined in: mongo-repository.ts:92

D

Promise<T>

VectorRepository.save

MongoRepository.save


saveAll(data): Promise<T[]>

Defined in: mongo-repository.ts:102

D[]

Promise<T[]>

VectorRepository.saveAll

MongoRepository.saveAll


protected sessionOptions(): object

Defined in: mongo-repository.ts:209

object

optional session?: ClientSession

MongoRepository.sessionOptions


protected toDocument(data, op, now?): Record<string, unknown>

Defined in: mongo-repository.ts:245

Map boundary data to a driver document: id becomes _id, timestamps applied.

Record<string, unknown>

"create" | "update"

Date = ...

Record<string, unknown>

MongoRepository.toDocument


protected toEntityField(field): string

Defined in: mongo-repository.ts:232

Translates a document field name back to its entity field name — the inverse of toField.

string

string

MongoRepository.toEntityField


protected toField(field): string

Defined in: mongo-repository.ts:227

Translates an entity field name to its document field name (fieldMap override, else identity).

string

string

MongoRepository.toField


protected toObjectId(id): ObjectId

Defined in: mongo-repository.ts:214

Convert a boundary Id to an ObjectId, rejecting malformed ids loudly.

Id

ObjectId

MongoRepository.toObjectId


updateById(id, data): Promise<T>

Defined in: mongo-repository.ts:113

Id

D

Promise<T>

VectorRepository.updateById

MongoRepository.updateById


upsertVector(id, vector, data): Promise<T>

Defined in: mongo-vector-repository.ts:73

Upsert a record with its embedding vector

Id

number[]

Partial<T>

Promise<T>

VectorRepository.upsertVector


withTransaction<R>(fn): Promise<R>

Defined in: mongo-repository.ts:190

Run a block of repository calls inside one MongoDB transaction. Requires the deployment to be a replica set — true for every Atlas cluster including free-tier M0; standalone servers throw the driver’s native error. The callback receives a session-bound copy of this repository; calls on the original instance are NOT part of the transaction.

R

(txRepo) => Promise<R>

Promise<R>

MongoRepository.withTransaction


protected wrapError(err): Error

Defined in: mongo-repository.ts:263

unknown

Error

MongoRepository.wrapError