You can use the knnVector type to index vector embeddings. The vector field can be represented as an array of numbers of the following types:
BSON
int32,int64, ordoubledata types for querying using the knnBeta operator.BSON
doubledata type for querying using the$vectorSearchstage.
You can use the knnBeta operator, which is now deprecated, and the $vectorSearch stage in your aggregation pipeline to query fields indexed as knnVector.
Note
You can't use the MongoDB Search Visual Editor in the Atlas UI to configure fields of type knnVector. Instead, use the MongoDB Search JSON Editor to configure fields of type knnVector.
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.
knnVector Type Limitations
You can't index fields inside arrays of documents or fields inside arrays of objects (MongoDB Search embeddedDocuments type) as knnVector type.
Define the Index for the dateFacet Type
The following is the JSON syntax for the dateFacet type. Replace the default index definition with the following. To learn more about the fields, see Field Properties.
1 { 2 "mappings": { 3 "dynamic": true|false, 4 "fields": { 5 "<field-name>": { 6 "type": "knnVector", 7 "dimensions": <number-of-dimensions>, 8 "similarity": "euclidean | cosine | dotProduct" 9 } 10 } 11 } 12 }
Configure dateFacet Field Properties
The knnVector type has the following options:
Option | Type | Necessity | Purpose |
|---|---|---|---|
| string | Required | Human-readable label that identifies this field type. Value must be |
| int | Required | Number of vector dimensions, which MongoDB Search enforces at index- and query-time. This value can't be greater than |
| string | Required | Vector similarity function to use to search for top K-nearest neighbors. Value can be one of the following:
|
Try an Example for the dateFacet Type
The following index definition for the sample_mflix.embedded_movies collection dynamically indexes all the dynamically indexable fields in the collection and statically indexes plot_embedding field as the knnVector type. The plot_embedding field contains embeddings created using OpenAI's text-embedding-ada-002 embeddings model. The index definition specifies 1536 vector dimensions and measures similarity using euclidean.
1 { 2 "mappings": { 3 "dynamic": true, 4 "fields": { 5 "plot_embedding": { 6 "type": "knnVector", 7 "dimensions": 1536, 8 "similarity": "euclidean" 9 } 10 } 11 } 12 }
If you load the sample data on your cluster and create the preceding MongoDB Search index for this collection, you can run $vectorSearch queries against this collection. To learn more about the sample queries that you can run, see $vectorSearch Examples.
Migrate to vector Type
To migrate your MongoDB Search indexes to the vector type, replace the knnVector type in your index definition for the vector field with the vector type.
Example
To use the vector type for the plot_embedding field in the sample index definition for the sample_mflix.embedded_movies collection, replace the index definition with the following:
1 { 2 "mappings": { 3 "dynamic": true, 4 "fields": { 5 "plot_embedding": { 6 "type": "vector", 7 "dimensions": 1536, 8 "similarity": "euclidean" 9 } 10 } 11 } 12 }
To run queries against the vector type field, you must use the vectorSearch (MongoDB Search Operator) instead of the knnBeta Operator.
Note
Your existing knnBeta Operator queries can continue to run against the vector type index. After migrating the index, we recommend immediately migrating your knnBeta operator queries to the vectorSearch (MongoDB Search Operator).