Definition
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.
Placement
$vectorSearch must be a middle stage of your pipeline. Do not use it as your pipeline's $source or sink.
Syntax
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 |
|---|---|---|---|
| document | Required | Document that specifies the target Atlas collection that |
| string | Required | Name of an Atlas connection in your Connection Registry. Atlas Stream Processing rejects any other connection type at parse time. |
| literal string | Required | Name of the target database. |
| literal string | Required | Name of the target collection containing the MongoDB Vector Search index. |
| 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 |
| array | field path expression | Conditional | Explicit vector embedding to search for matching documents against. |
| document | Conditional | Document that specifies the text query for auto embedding. |
| string | field path expression | Conditional | Query text to use for auto-embedding. |
| string | Optional | Embedding model MongoDB Vector Search uses to convert When you omit |
| string | Required | Name of the MongoDB Vector Search index to query. |
| string | Required | Field in the target collection that contains the vector embeddings to search against. |
| int | Conditional | Number of candidates that MongoDB Vector Search considers during the search. Required unless |
| int | Required | Maximum number of results to return. Must not exceed WARNING: If |
| document | Optional | Pre-filter expression to apply before the vector search. For supported operators, see Filter. WARNING: If |
| boolean | Optional | Flag that specifies whether to run an ANN or ENN search. Provide this field if you omit Value can be one of the following:
Defaults to |
| document | Optional | Document with a |
| array | Optional | Additional aggregation stages that Atlas Stream Processing runs against the search results, before attaching them to the input document. |
| document | Optional | Variables that Atlas Stream Processing evaluates against each input document. Each value can be a literal, a field path, or an expression. |
Behavior
Query Modes
$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.
Error Handling
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.
Process Results
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>.
Examples
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.