An approximate nearest neighbor (ANN) algorithm finds vectors close to a query vector without comparing it with every stored vector. In Atlas Vector Search, ANN supports fast similarity searches for uses such as semantic search and product recommendations. It trades some precision for speed; exact nearest neighbor (ENN) search compares every vector and costs more on large data sets. section 12.2 section 12.7
Atlas Vector Search uses Apache Lucene’s Hierarchical Navigable Small Worlds (HNSW) algorithm for ANN. HNSW connects similar vectors in a layered graph. A search starts in a sparse upper layer, moves toward nearby nodes, then descends through denser layers to refine the result. This avoids a brute-force scan. section 12.2
To run an ANN query with Atlas Vector Search:
- Create a vector search index on the field holding stored embeddings. section 12.8
- Embed the query with the same model used for the stored data. Atlas did not provide text-to-embedding conversion at the time described, so this step may require an external system. section 12.3.1
- Use $vectorSearch with the index name, embedding field as path, and numeric queryVector. Set exact to false or omit it; ANN is the default. Choose limit for returned results and numCandidates for vectors considered. section 12.3.1 section 12.3
db.embedded_movies.aggregate([
{
"$vectorSearch": {
"index": "MongoDB-in-Action-VectorSearchIndex",
➥// #1
"path": "plot_embedding", // #2
"queryVector": [<array-of-numbers>], // #3
"numCandidates": <number-of-candidates>, // #4
"exact": false, // #5
"limit": <number-of-results> // #6
}
}
])
This $vectorSearch outline shows an approximate query against movie plot embeddings. Replace the placeholders with an embedding array, candidate count, and result limit. section 12.3.1
Tune numCandidates with the speed–precision trade-off in mind: considering more candidates can improve precision but take longer. Prefer ANN for routine similarity queries and use ENN when exactness is necessary. For performance, avoid indexing during queries, omit large vector fields from returned documents, and provide enough memory for vector data and indexes. section 12.3 section 12.7




