Vector search

$vectorSearch with vector indexes on the same nodes as your documents.

$vectorSearch finds the documents whose vectors are closest to your query's. You store the vector as another field of the document and create a search index of type vectorSearch.

Create the index

JavaScript
db.products.createSearchIndex({
  name: "vector",
  type: "vectorSearch",
  definition: {
    fields: [
      { type: "vector", path: "embedding", numDimensions: 768, similarity: "cosine" },
      { type: "filter", path: "category" },
    ],
  },
});
JavaScript
db.products.aggregate([
  {
    $vectorSearch: {
      index: "vector",
      path: "embedding",
      queryVector: queryEmbedding,
      numCandidates: 200,
      limit: 10,
      filter: { category: "books" },
    },
  },
  { $project: { name: 1, score: { $meta: "vectorSearchScore" } } },
]);

Exact or approximate

By default the search is approximate: it walks an HNSW graph per range. With exact: true it compares against every vector and the result is the same on any replica. Without exact, two replicas can return different documents at the same timestamp, because each links its graph as it applies the changes.

A full guide with embeddings is in Semantic search with $vectorSearch.