For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$vectorSearch Aggregation Stage

The $vectorSearch stage enriches each streaming document with results from a MongoDB Vector Search query. For each input document, the stage runs a $vectorSearch query against a collection and attaches results to a field you specify.

$vectorSearch must be a middle stage of your pipeline. Do not use it as your pipeline's $source or sink.

The $vectorSearch pipeline stage has the following prototype form:

{
"$vectorSearch": {
"from": {
"connectionName": "<registered-atlas-connection>",
"db": "<database>",
"coll": "<collection>"
},
"as": "<output-field>",
"queryVector": [<number>, ...] | <expression>,
"query": {
"text": "<string>" | <expression>
},
"model": "<embedding-model>",
"index": "<vector-index-name>",
"path": "<field-containing-vectors>",
"numCandidates": <int>,
"limit": <int>,
"filter": { ... },
"exact": <bool>,
"searchNodePreference": {
"key": "<preference>"
},
"pipeline": [ { $<aggregation-stage>: { ... } }, ... ],
"let": {
"<variable>": <expression>,
. . .
}
}
}

The $vectorSearch stage takes a document with the following fields:

Field
Type
Necessity
Description

from

document

Required

Document that specifies the target Atlas collection that $vectorSearch queries for each input document.

from.connectionName

string

Required

Name of an Atlas connection in your Connection Registry. Atlas Stream Processing rejects any other connection type at parse time.

from.db

literal string

Required

Name of the target database.

from.coll

literal string

Required

Name of the target collection containing the MongoDB Vector Search index.

as

string

Required

Output field on the streaming document for the search results array. If MongoDB Vector Search returns no matching documents, Atlas Stream Processing sets this field to an empty array.

WARNING: If this field is empty or collides with the stream metadata field name, Atlas Stream Processing reports a StreamProcessorInvalidOptions error at start time.

queryVector

array | field path expression

Conditional

Explicit vector embedding to search for matching documents against.

query

document

Conditional

Document that specifies the text query for auto embedding.

query.text

string | field path expression

Conditional

Query text to use for auto-embedding.

model

string

Optional

Embedding model MongoDB Vector Search uses to convert query.text to a vector. Valid only alongside query.

When you omit model, MongoDB Vector Search uses the model configured in the target MongoDB Vector Search index's definition.

index

string

Required

Name of the MongoDB Vector Search index to query.

path

string

Required

Field in the target collection that contains the vector embeddings to search against.

numCandidates

int

Conditional

Number of candidates that MongoDB Vector Search considers during the search. Required unless exact is true. To learn how to choose a value, see numCandidates Selection.

limit

int

Required

Maximum number of results to return. Must not exceed numCandidates.

WARNING: If limit exceeds numCandidates, every document fails at runtime with a PlanExecutor error.

filter

document

Optional

Pre-filter expression to apply before the vector search. For supported operators, see Filter.

WARNING: If filter uses an unsupported operator, every document fails at runtime with a PlanExecutor error.

exact

boolean

Optional

Flag that specifies whether to run an ANN or ENN search. Provide this field if you omit numCandidates.

Value can be one of the following:

Defaults to false.

searchNodePreference

document

Optional

Document with a key field that Atlas Stream Processing uses to route the query to a specific search node. The key value must be a non-empty string.

pipeline

array

Optional

Additional aggregation stages that Atlas Stream Processing runs against the search results, before attaching them to the input document.

let

document

Optional

Variables that Atlas Stream Processing evaluates against each input document. Each value can be a literal, a field path, or an expression. pipeline stages can reference the resolved values as $$<variable>.

$vectorSearch supports two mutually exclusive query modes: queryVector for vector embeddings and query.text for auto embedding. In either mode, you can specify a literal value or a field path expression, so a single stream processor can issue a different query for each input document.

If the target MongoDB Vector Search index doesn’t exist, the stream processor enters an error state when $vectorSearch first runs. Atlas Stream Processing reports a MongoServerError error and stops processing documents.

If queryVector or query.text evaluates to the wrong type at runtime, Atlas Stream Processing routes only the offending document to the dead letter queue, if configured, or drops it otherwise. The stream processor continues to process later documents. For the DLQ document schema, see Dead Letter Queue.

Use pipeline to further process the search results before Atlas Stream Processing attaches them to the input document. Atlas Stream Processing forwards pipeline to MongoDB Vector Search verbatim. To reference a value from the input document inside pipeline, pass it through let and reference it as $$<variable>.

The following examples query the sample_mflix dataset, which contains data on movies and movie theaters. To run them, load the sample data into your Atlas cluster and create an MongoDB Vector Search index on the embedded_movies collection.