vector-search-stage
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset
Available tabs: other tabs: async, sync -->
Run Vector Search ANN and ENN Queries
A MongoDB Vector Search query takes the form of an [aggregation pipeline](https://www.mongodb.com/docs/manual/aggregation.md#std-label-aggregation that uses [$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch as the first stage. This page explains the syntax, options, and behavior of the [$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch stage.
The $vectorSearch stage performs a semantic search for a query vector on the specified field or fields. The field or fields must be indexed as the MongoDB Vector Search [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search type index.
Note:
MongoDB Vector Search supports ANN (Approximate Nearest Neighbor) search on Atlas Clusters running MongoDB v6.0.11, v7.0.2, or later and ENN (Exact Nearest Neighbor) search on Clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. You can also use MongoDB Vector Search with [self-managed](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition or local Atlas deployments that you create with the Atlas CLI.
The $vectorSearch stage performs a semantic search for a query vector on the specified field or fields. The field or fields must be indexed as the MongoDB Vector Search [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search type index.
The $vectorSearch stage is used to perform a semantic search for a natural language query on the specified field. When using automated embeddings, the field or fields must be indexed as the MongoDB Vector Search [autoEmbed](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-auto-embed type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search type index.
Important:
Automated Embeddings is available as a Preview feature. The feature and the corresponding documentation might change at any time during the Preview period. Do not use this feature in your production environment. We do not use any customer data from this feature to train our models at this time. To learn more, see Preview Features.
The $vectorSearch stage is used to perform a semantic search for a natural language query on the specified field. When using automated embeddings, the field or fields must be indexed as the MongoDB Vector Search [autoEmbed](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-auto-embed type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search type index.
Supported Clients
You can run [$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch queries by using the Atlas UI, MongoDB Compass, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, and any MongoDB driver.
You can also use MongoDB Vector Search with local Atlas deployments that you create with the Atlas CLI. To learn more, see Create a Local Atlas Deployment.
[$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch is supported only on Atlas clusters running the following MongoDB versions:
-
v6.0.11
-
v7.0.2 and later (including RCs (Release Candidates)).
You can run [$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch queries by using MongoDB Compass, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, and any MongoDB driver.
You can run [$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch queries by using MongoDB Compass, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, and the following MongoDB drivers:
| MongoDB Driver | Version |
|---|---|
| C# | 3.x or later |
| Go | 2.x or later |
| Java | 5.x or later |
| Node | 6.6.0 or later |
| Python | 4.7 or later |
| C++ | 3.11.0 or later |
| Rust | 3.1.0 or later |
Syntax
The field that you want to search must be indexed as MongoDB Vector Search [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search index type.
A $vectorSearch pipeline has the following prototype form:
{
"$vectorSearch": {
"exact": true | false,
"index": "<index-name>",
"limit": <number-of-results>,
"numCandidates": <number-of-candidates>,
"path": "<field-to-search>",
"queryVector": [<array-of-numbers>],
"filter": {<filter-specification>},
"parentFilter": {<parent-filter-specification>},
"nestedOptions": {
"scoreMode": "max | avg"
},
"explainOptions": {
"traceDocumentIds": [<array-of-documentIDs>]
},
"returnStoredSource": true | false,
"searchNodePreference": {
"key": <preference-string>
}
}
}
The field that you want to search must be indexed as MongoDB Vector Search [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search index type.
A $vectorSearch pipeline has the following prototype form:
{
"$vectorSearch": {
"exact": true | false,
"filter": {<filter-specification>},
"index": "<index-name>",
"limit": <number-of-results>,
"numCandidates": <number-of-candidates>,
"path": "<field-to-search>",
"queryVector": [<array-of-numbers>],
"explainOptions": {
"traceDocumentIds": [<array-of-documentIDs>]
},
"parentFilter": {<parent-filter-specification>},
"nestedOptions": {
"scoreMode": "max | avg"
},
"returnStoredSource": true | false
}
}
The field that you want to search must be indexed as MongoDB Vector Search [autoEmbed](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-auto-embed type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search index type.
A $vectorSearch pipeline has the following prototype form:
{
"$vectorSearch": {
"exact": true | false,
"filter": {<filter-specification>},
"index": "<index-name>",
"limit": <number-of-results>,
"model": "<model-name>",
"numCandidates": <number-of-candidates>,
"path": "<field-to-search>",
"query": {
"text": "<query-text>"
},
"parentFilter": {<parent-filter-specification>},
"nestedOptions": {
"scoreMode": "max | avg"
},
"searchNodePreference": {
"key": <preference-string>
}
}
}
The field that you want to search must be indexed as MongoDB Vector Search [autoEmbed](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-auto-embed type inside a [vectorSearch](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search index type.
A $vectorSearch pipeline has the following prototype form:
{
"$vectorSearch": {
"exact": true | false,
"filter": {<filter-specification>},
"index": "<index-name>",
"limit": <number-of-results>,
"model": "<model-name>",
"numCandidates": <number-of-candidates>,
"path": "<field-to-search>",
"query": {
"text": "<query-text>"
}
}
}
Fields
The $vectorSearch stage takes a document with the following fields:
| Field | Type | Necessity | Description |
|---|---|---|---|
exact |
Boolean | Optional | This is required if numCandidates is omitted. Flag that specifies whether to run ENN (Exact Nearest Neighbor) or ANN (Approximate Nearest Neighbor) search. Value can be one of the following: false - to run ANN (Approximate Nearest Neighbor) search; true - to run ENN (Exact Nearest Neighbor) search If omitted, defaults to false. MongoDB Vector Search supports ANN (Approximate Nearest Neighbor) search on Atlas Clusters running MongoDB v6.0.11, v7.0.2, or later and ENN (Exact Nearest Neighbor) search on Clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. You can also use MongoDB Vector Search with [self-managed](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition or local Atlas deployments that you create with the Atlas CLI. To learn more about these search types, see [Vector Search Types.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline |
explainOptions |
Object | Optional | Trace a list of vectors (identified by theirs _id) in an explainf |
explainOptions.``traceDocumentIds |
Array of objectIDs | Required | List of document _ids. |
filter |
Object | Optional | MQL (MongoDB Query Language) expression that uses an indexed field for pre-filtering the documents. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter If you specify a nestedRoot in the index definition, the filter option applies to fields at the nested level. Use parentFilter to filter on root-level fields. If you specify both, MongoDB Vector Search combines them using AND. |
index |
String | Required | Name of the MongoDB Vector Search index to use. MongoDB Vector Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster. |
limit |
Int | Required | Number (of type int only) of documents to return in the results. This value can't exceed the value of numCandidates if you specify numCandidates. |
nestedOptions |
Object | Optional | Configure how MongoDB Vector Search scores documents that contain nested arrays. |
nestedOptions.``scoreMode |
String | Optional | Specifies how to score documents that contain nested arrays. Value can be one of the following: avg - average the scores of the nested array elements.; max - use the maximum score of the nested array elements. |
numCandidates |
Int | Conditional | This field is required if exact is false or omitted. Number of nearest neighbors to use during the search. Value must be less than or equal to (<=) 10000. You can't specify a number less than the number of documents to return (limit). We recommend that you specify a number at least 20 times higher than the number of documents to return (limit) to increase accuracy. This overrequest pattern is the recommended way to trade off latency and [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall in your ANN (Approximate Nearest Neighbor) searches, and we recommend tuning this parameter based on your specific dataset size and query requirements. To learn more about other variables that might impact this parameter, see [numCandidates Selection.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-num-candidates |
parentFilter |
Object | Optional | MQL (MongoDB Query Language) expression that uses an indexed top-level field for pre-filtering the documents. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter The parentFilter option is only valid if you specify a nested field (nestedRoot) in the index definition. If you specify the parentFilter, MongoDB Vector Search filters the top-level documents before filtering the nested documents. |
path |
String | Required | Indexed [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type field to search. Use dot notation to specify path to embedded fields. You can query only one [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type field per $vectorSearch stage. |
queryVector |
Array of Numbers | Required | Array of numbers of float32, BSON (Binary Javascript Object Notation) [BinData](https://www.mongodb.com/docs/manual/reference/method/BinData.md#std-label-server-binData-method vectors with subtype float32, or BSON (Binary Javascript Object Notation) [BinData](https://www.mongodb.com/docs/manual/reference/method/BinData.md#std-label-server-binData-method vectors with subtype int1 or int8 type that represent the query vector. To learn more about generating BSON (Binary Javascript Object Notation) binData vectors with subtype float32, int8, int1, see [How to Ingest Pre-Quantized Vectors.](https://www.mongodb.com/docs/vector-search/about/vector-quantization.md#std-label-avs-bindata-vector-subtype The array size must match the number of vector dimensions specified in the [index definition](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search for the field. You must embed your query with the same model that you used to embed the data. You can query your embeddings with full-fidelity vectors, if the vector subtype is the same. This is only possible with binData vectors with subtype float32. If you use any other subtype (int8 or int1), MongoDB Vector Search doesn't return any results or errors. |
returnStoredSource |
Boolean | Optional | Flag that specifies whether to perform a full document lookup on the backend database (false) or return only stored source fields directly from MongoDB Vector Search (true). If omitted, defaults to false. To learn more, see [Return Stored Source Fields.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-return-stored-source |
searchNodePreference |
Object | Optional | Document that enables preferential routing for this query. If you set a value for this field, Atlas executes this query against the same search node each time as long as that value remains the same, overriding the [default query routing](https://www.mongodb.com/docs/search/query/query-ref.md#std-label-about-mongot behavior. This document contains a key-value pair where the value of key is an arbitrary string. IMPORTANT: Enabling this setting can improve the consistency of your search results, but doesn't guarantee perpetual consistency. Changes to your data set, index structure, cluster topology, or preferred node availability might still cause inconsistencies. |
| Field | Type | Necessity | Description |
|---|---|---|---|
exact |
Boolean | Optional | This is required if numCandidates is omitted. Flag that specifies whether to run ENN (Exact Nearest Neighbor) or ANN (Approximate Nearest Neighbor) search. Value can be one of the following: false - to run ANN (Approximate Nearest Neighbor) search; true - to run ENN (Exact Nearest Neighbor) search If omitted, defaults to false. MongoDB Vector Search supports ANN (Approximate Nearest Neighbor) search on Atlas Clusters running MongoDB v6.0.11, v7.0.2, or later and ENN (Exact Nearest Neighbor) search on Clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. You can also use MongoDB Vector Search with [self-managed](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition or local Atlas deployments that you create with the Atlas CLI. To learn more about these search types, see [Vector Search Types.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline |
explainOptions |
Object | Optional | Trace a list of vectors (identified by theirs _id) in an explainf |
explainOptions.``traceDocumentIds |
Array of objectIDs | Required | List of document _ids. |
filter |
Object | Optional | MQL (MongoDB Query Language) expression that compares an indexed field to use as a pre-filter. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter |
index |
String | Required | Name of the MongoDB Vector Search index to use. MongoDB Vector Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster. |
limit |
Int | Required | Number (of type int only) of documents to return in the results. This value can't exceed the value of numCandidates if you specify numCandidates. |
nestedOptions |
Object | Optional | Configure how MongoDB Vector Search scores documents that contain nested arrays. |
nestedOptions.``scoreMode |
String | Optional | Specifies how to score documents that contain nested arrays. Value can be one of the following: avg - average the scores of the nested array elements.; max - use the maximum score of the nested array elements. |
numCandidates |
Int | Conditional | This field is required if exact is false or omitted. Number of nearest neighbors to use during the search. Value must be less than or equal to (<=) 10000. You can't specify a number less than the number of documents to return (limit). We recommend that you specify a number at least 20 times higher than the number of documents to return (limit) to increase accuracy. This overrequest pattern is the recommended way to trade off latency and [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall in your ANN (Approximate Nearest Neighbor) searches, and we recommend tuning this parameter based on your specific dataset size and query requirements. To learn more about other variables that might impact this parameter, see [numCandidates Selection.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-num-candidates |
parentFilter |
Object | Optional | MQL (MongoDB Query Language) expression that uses an indexed top-level field for pre-filtering the documents. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter The parentFilter option is only valid if you specify a nested field (nestedRoot) in the index definition. If you specify the parentFilter, MongoDB Vector Search filters the top-level documents before filtering the nested documents. |
path |
String | Required | Indexed [vector](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector type field to search. |
queryVector |
Array of Numbers | Required | Array of numbers of float32, BSON (Binary Javascript Object Notation) [BinData](https://www.mongodb.com/docs/manual/reference/method/BinData.md#std-label-server-binData-method vectors with subtype float32, or BSON (Binary Javascript Object Notation) [BinData](https://www.mongodb.com/docs/manual/reference/method/BinData.md#std-label-server-binData-method vectors with subtype int1 or int8 type that represent the query vector. To learn more about generating BSON (Binary Javascript Object Notation) binData vectors with subtype float32, int8, int1, see [How to Ingest Pre-Quantized Vectors.](https://www.mongodb.com/docs/vector-search/about/vector-quantization.md#std-label-avs-bindata-vector-subtype The array size must match the number of vector dimensions specified in the [index definition](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-vector-search for the field. You must embed your query with the same model that you used to embed the data. You can query your embeddings with full-fidelity vectors if the vector subtype is the same. This is only possible with binData vectors with subtype float32. If you use any other subtype (int8 or int1), MongoDB Vector Search doesn't return any results or errors. |
returnStoredSource |
Boolean | Optional | Flag that specifies whether to perform a full document lookup on the backend database (false) or return only stored source fields directly from MongoDB Vector Search (true). If omitted, defaults to false. To learn more, see [Return Stored Source Fields.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-return-stored-source-self-managed |
| Field | Type | Necessity | Description |
|---|---|---|---|
exact |
Boolean | Optional | This is required if numCandidates is omitted. Flag that specifies whether to run ENN (Exact Nearest Neighbor) or ANN (Approximate Nearest Neighbor) search. Value can be one of the following: false - to run ANN (Approximate Nearest Neighbor) search; true - to run ENN (Exact Nearest Neighbor) search If omitted, defaults to false. MongoDB Vector Search supports ANN (Approximate Nearest Neighbor) search on Atlas Clusters running MongoDB v6.0.11, v7.0.2, or later and ENN (Exact Nearest Neighbor) search on Clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. You can also use MongoDB Vector Search with [self-managed](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition or local Atlas deployments that you create with the Atlas CLI. To learn more about these search types, see [Vector Search Types.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline |
filter |
Object | Optional | MQL (MongoDB Query Language) expression that compares an indexed field to use as a pre-filter. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter |
index |
String | Required | Name of the MongoDB Vector Search index to use. MongoDB Vector Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster. |
limit |
Int | Required | Number (of type int only) of documents to return in the results. This value can't exceed the value of numCandidates if you specify numCandidates. |
model |
String | Optional | Name of the embedding model to use for generating embeddings for the query string. The embedding model must be compatible with the embedding model specified in the index definition for the field against which you want to run the query. All the models in the voyage-4 family of models are compatible with each other, but the voyage-code-4 and voyage-code-3 models are not compatible with any other model. Value can be one of the following: voyage-4-large - Highest-quality retrieval across languages and domains.; voyage-4 - Balanced model for multilingual use and general-purpose retrieval accuracy. (Recommended); voyage-4-lite - Lightweight, faster model optimized for latency and lower cost.; voyage-code-4 - Optimized for code retrieval. (Recommended for code) This embedding model is not compatible with any other supported embedding model. Therefore, you can't use this model if you indexed the field using a different embedding model.; voyage-code-3 - Legacy model optimized for code retrieval. Use voyage-code-4 instead. This embedding model is not compatible with any other supported embedding model. Therefore, you can't use this model if you indexed the field using a different embedding model. If omitted, defaults to the embedding model specified in the index definition. If you want to query using the queryVector parameter, you must omit this field. |
nestedOptions |
Object | Optional | Configure how MongoDB Vector Search scores documents that contain nested arrays. |
nestedOptions.``scoreMode |
String | Optional | Specifies how to score documents that contain nested arrays. Value can be one of the following: avg - average the scores of the nested array elements.; max - use the maximum score of the nested array elements. |
numCandidates |
Int | Conditional | This field is required if exact is false or omitted. Number of nearest neighbors to use during the search. Value must be less than or equal to (<=) 10000. You can't specify a number less than the number of documents to return (limit). We recommend that you specify a number at least 20 times higher than the number of documents to return (limit) to increase accuracy. This overrequest pattern is the recommended way to trade off latency and [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall in your ANN (Approximate Nearest Neighbor) searches, and we recommend tuning this parameter based on your specific dataset size and query requirements. To learn more about other variables that might impact this parameter, see [numCandidates Selection.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-num-candidates |
parentFilter |
Object | Optional | MQL (MongoDB Query Language) expression that uses an indexed top-level field for pre-filtering the documents. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter The parentFilter option is only valid if you specify a nested field (nestedRoot) in the index definition. If you specify the parentFilter, MongoDB Vector Search filters the top-level documents before filtering the nested documents. |
path |
String | Required | Indexed [autoEmbed](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-auto-embed type field to search. |
query |
Object | Optional | Object that specifies the query context. MongoDB Vector Search supports text query. This is mutually exclusive with queryVector. |
query.``text |
String | Required | Query text for which you want to perform the semantic search. MongoDB Vector Search automatically generates vector embeddings for the specified text using the model specified in the query or in the index definition. The embedding model specified in the query takes precedence. |
queryVector |
Array of Numbers | Optional | Array of vector embeddings or floating point numbers generated using a Voyage AI embedding model to perform a semantic search. To retrieve correct results, you must use a Voyage AI embedding model that is compatible with the Voyage AI embeddings model specified in the index definition for the field that you want to query. The format of the queryVector must match the quantization type configured in the index definition. If the formats don't match, MongoDB Vector Search returns empty results. Provide the queryVector in the following format for each quantization type: scalar - Provide a scalar-quantized vector. Because scalar is the default, provide a scalar-quantized vector when you omit the quantization field from the index definition.; float - Provide a float vector.; binary - Provide a float vector.; binaryNoRescore - Provide a binary-quantized vector. This is mutually exclusive with model and query. |
searchNodePreference |
Object | Optional | Document that enables preferential routing for this query. If you set a value for this field, Atlas executes this query against the same search node each time as long as that value remains the same, overriding the [default query routing](https://www.mongodb.com/docs/search/query/query-ref.md#std-label-about-mongot behavior. This document contains a key-value pair where the value of key is an arbitrary string. IMPORTANT: Enabling this setting can improve the consistency of your search results, but doesn't guarantee perpetual consistency. Changes to your data set, index structure, cluster topology, or preferred node availability might still cause inconsistencies. |
| Field | Type | Necessity | Description |
|---|---|---|---|
exact |
Boolean | Optional | This is required if numCandidates is omitted. Flag that specifies whether to run ENN (Exact Nearest Neighbor) or ANN (Approximate Nearest Neighbor) search. Value can be one of the following: false - to run ANN (Approximate Nearest Neighbor) search; true - to run ENN (Exact Nearest Neighbor) search If omitted, defaults to false. MongoDB Vector Search supports ANN (Approximate Nearest Neighbor) search on Atlas Clusters running MongoDB v6.0.11, v7.0.2, or later and ENN (Exact Nearest Neighbor) search on Clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. You can also use MongoDB Vector Search with [self-managed](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition or local Atlas deployments that you create with the Atlas CLI. To learn more about these search types, see [Vector Search Types.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline |
filter |
Object | Optional | MQL (MongoDB Query Language) expression that compares an indexed field to use as a pre-filter. You can filter on boolean, date, objectId, numeric, string, and UUID values, including arrays of these types. To learn which MQL operators MongoDB Vector Search supports in your filter, see [MongoDB Vector Search Filtering.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-vectorSearch-agg-pipeline-filter |
index |
String | Required | Name of the MongoDB Vector Search index to use. MongoDB Vector Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster. |
limit |
Int | Required | Number (of type int only) of documents to return in the results. This value can't exceed the value of numCandidates if you specify numCandidates. |
model |
String | Optional | Name of the embedding model to use for generating embeddings for the query string. The embedding model must be compatible with the embedding model specified in the index definition for the field against which you want to run the query. All the models in the voyage-4 family of models are compatible with each other, but the voyage-code-3 is not compatible with the other models. Value can be one of the following: voyage-4-large - Highest-quality retrieval across languages and domains.; voyage-4 - Balanced model for multilingual use and general-purpose retrieval accuracy. (Recommended); voyage-4-lite - Lightweight, faster model optimized for latency and lower cost.; voyage-code-3 - Optimized for code retrieval. This embedding model is not compatible with voyage-4-large, voyage-4, and voyage-4-lite embedding models. Therefore, this model can't be used if you indexed the field using the other supported embedding models. If omitted, defaults to the embedding model specified in the index definition. If you want to query using the queryVector parameter, you must omit this field. |
numCandidates |
Int | Conditional | This field is required if exact is false or omitted. Number of nearest neighbors to use during the search. Value must be less than or equal to (<=) 10000. You can't specify a number less than the number of documents to return (limit). We recommend that you specify a number at least 20 times higher than the number of documents to return (limit) to increase accuracy. This overrequest pattern is the recommended way to trade off latency and [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall in your ANN (Approximate Nearest Neighbor) searches, and we recommend tuning this parameter based on your specific dataset size and query requirements. To learn more about other variables that might impact this parameter, see [numCandidates Selection.](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-num-candidates |
path |
String | Required | Indexed [autoEmbed](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-types-auto-embed type field to search. |
query |
Object | Optional | Object that specifies the query context. MongoDB Vector Search supports text query. This is mutually exclusive with queryVector. |
query.``text |
String | Required | Query text for which you want to perform the semantic search. MongoDB Vector Search automatically generates vector embeddings for the specified text using the model specified in the query or in the index definition. The embedding model specified in the query takes precedence. |
queryVector |
Array of Numbers | Optional | Array of vector embeddings or floating point numbers generated using a Voyage AI embedding model to perform a semantic search. To retrieve correct results, you must use a Voyage AI embedding model that is compatible with the Voyage AI embeddings model specified in the index definition for the field that you want to query. The format of the queryVector must match the quantization type configured in the index definition. If the formats don't match, MongoDB Vector Search returns empty results. Provide the queryVector in the following format for each quantization type: scalar - Provide a scalar-quantized vector. Because scalar is the default, provide a scalar-quantized vector when you omit the quantization field from the index definition.; float - Provide a float vector.; binary - Provide a float vector.; binaryNoRescore - Provide a binary-quantized vector. This is mutually exclusive with model and query. |
Vector Search Types
When you define a [$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch stage, you can use the exact field to specify whether to run an ANN (Approximate Nearest Neighbor) or ENN (Exact Nearest Neighbor) search.
ANN Search
Approximate Nearest Neighbors (ANN) search
For Approximate Nearest Neighbors (ANN) search, MongoDB Vector Search finds vector embeddings in your data that are closest to the vector embedding in your query based on their proximity in multi-dimensional space and based on the number of neighbors that it considers. It uses the Hierarchical Navigable Small Worlds algorithm and finds the vector embeddings most similar to the vector embedding in your query without scanning every vector. Therefore, ANN (Approximate Nearest Neighbor) search is ideal for querying large datasets without significant filtering.
Note:
Optimal [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall for ANN (Approximate Nearest Neighbor) search is typically considered to be around 90-95% overlap in results with ENN (Exact Nearest Neighbor) search but with significantly lower latency. This provides a good balance between accuracy and performance. To achieve this with MongoDB Vector Search, [tune](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#std-label-avs-num-candidates the numCandidates parameter at query time.
numCandidates Selection
You must specify the numCandidates field to run ANN (Approximate Nearest Neighbor) search. This field determines how many nearest neighbors MongoDB Vector Search considers during the search.
When you perform a vector search using the Hierarchical Navigable Small Worlds index structure, MongoDB Vector Search accumulates results by populating a priority queue. The numCandidates parameter controls the size of this queue, which determines how long to search before returning the top limit results. A larger queue (higher numCandidates) allows the search to explore more of the Hierarchical Navigable Small Worlds graph, potentially finding better matches at the cost of increased query latency.
We recommend that you specify a numCandidates number at least 20 times higher than the number of documents to return (limit) to increase accuracy and reduce discrepancies between your ENN (Exact Nearest Neighbor) and ANN (Approximate Nearest Neighbor) query results. For example, if you set limit to return 5 results, consider setting numCandidates to 100 as a starting point. To learn more, see [How to Measure the Accuracy of Your Query Results.](https://www.mongodb.com/docs/vector-search/query/improve-accuracy.md#std-label-avs-improve-results
This overrequest pattern is the recommended way to trade off latency and [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall in your ANN (Approximate Nearest Neighbor) searches. However, we recommend tuning the numCandidates parameter based on your specific dataset size and query requirements. To ensure that you get accurate results, consider the following variables:
-
Index Size: Larger collections typically require higher
numCandidatesvalues to maintain recall. A collection with millions of vectors might need significantly more candidates than one with thousands of vectors. -
Limit Value: Because
numCandidatesis highly correlated with the index size, lowlimitvalues require proportionally highernumCandidatesvalues to maintain recall. -
Vector Quantization: [Quantized vectors](https://www.mongodb.com/docs/vector-search/indexes/vector-search-type.md#std-label-avs-quantization reduce storage at the cost of accuracy. Using quantized vectors (
int8orint1subtypes) might require highernumCandidatesvalues compared to full precisionfloat32vectors to maintain similar recall. -
Index Size: Larger collections typically require higher
numCandidatesvalues to maintain recall. A collection with millions of vectors might need significantly more candidates than one with thousands of vectors. -
Limit Value: Because
numCandidatesis highly correlated with the index size, lowlimitvalues require proportionally highernumCandidatesvalues to maintain recall.
ENN Search
Exact Nearest Neighbors (ENN) search
For an Exact Nearest Neighbors (ENN) search, MongoDB Vector Search exhaustively searches all the indexed vector embeddings by calculating the distance between all the embeddings and finds the exact nearest neighbor for the vector embedding in your query. This is computationally intensive and might negatively impact query latency. Therefore, we recommend ENN (Exact Nearest Neighbor) searches for the following use-cases:
-
You want to determine the [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall and accuracy of your ANN (Approximate Nearest Neighbor) query using the ideal, exact results for the ENN (Exact Nearest Neighbor) query.
-
You want to query less than 10000 documents without having to tune the number of nearest neighbors to consider.
-
Your want to include selective pre-filters in your query against collections where less than 5% of your data meets the given pre-filter.
If you enable automatic quantization, MongoDB Vector Search uses only the full-fidelity vectors for ENN (Exact Nearest Neighbor) queries.
-
You want to determine the [recall](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-recall and accuracy of your ANN (Approximate Nearest Neighbor) query using the ideal, exact results for the ENN (Exact Nearest Neighbor) query.
-
You want to query less than 10000 documents without having to tune the number of nearest neighbors to consider.
-
Your want to include selective pre-filters in your query against collections where less than 5% of your data meets the given pre-filter.
Considerations
[$vectorSearch](https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage.md#mongodb-pipeline-pipe.-vectorSearch must be the first stage of any pipeline where it appears.
Best Practices
Automated Embedding imposes rate limits on the number of requests you can make and the number of tokens you can use within a specific time frame. This is measured in tokens per minute (TPM) and requests per minute (RPM) to ensure fair usage, prevent abuse, and maintain the stability and performance of the endpoint service for all users. To learn more, see [Rate Limits](https://www.mongodb.com/docs/vector-search/crud-embeddings/automated-embedding/models.md#std-label-auto-embed-rate-limits. You can request for a rate limit increase by [contacting support.](https://www.mongodb.com/docs/atlas/support.md#std-label-request-support
You can use a different model in your query, which is compatible with the model used to create the index. This allows you to have different price and retrieval quality
(本文超出站点单文档体积上限,已截断)
