Docs Menu
Docs Home
/ /

Run Vector Search Queries

A MongoDB Vector Search query takes the form of an aggregation pipeline that uses $vectorSearch as the first stage. This page explains the syntax, options, and behavior of the $vectorSearch stage.

$vectorSearch

The $vectorSearch stage takes a document with the following fields:

When you define a $vectorSearch stage, you can use the exact field to specify whether to run an ANN or ENN search.

$vectorSearch must be the first stage of any pipeline where it appears.

$vectorSearch can't be used in view definition and the following pipeline stages:

  • $lookup sub-pipeline [1]

  • $facet pipeline stage

[1] You can pass the results of $vectorSearch to this stage.

To learn more about these MongoDB Vector Search field types, see How to Index Fields for Vector Search.

MongoDB Vector Search assigns a score, in a fixed range from 0 to 1 (where 0 indicates low similarity and 1 indicates high similarity), to every document that it returns.

Each returned document includes the score as metadata. To return each document's score along with the result set, use a $project stage in your aggregation pipeline and configure the score as a field to project. In the score field, specify a $meta expression with the value vectorSearchScore. The syntax is as follows:

1db.<collection>.aggregate([
2 {
3 "$vectorSearch": {
4 <query-syntax>
5 }
6 },
7 {
8 "$project": {
9 "<field-to-include>": 1,
10 "<field-to-exclude>": 0,
11 "score": { "$meta": "vectorSearchScore" }
12 }
13 }
14])

Note

You can use vectorSearchScore as a score $meta expression only after the $vectorSearch pipeline stage. If you use vectorSearchScore after any other query, MongoDB logs a warning starting in MongoDB v8.2.

Note

Pre-filtering your data doesn't affect the score that MongoDB Vector Search returns using vectorSearchScore for $vectorSearch queries.

The $vectorSearch filter option matches BSON boolean, date, objectId, numeric, string, and UUID values, including arrays of these types.

You must index the fields that you want to filter your data by as the filter type in a vectorSearch type index definition. Filtering your data is useful to narrow the scope of your semantic search and ensure that not all vectors are considered for comparison.

MongoDB Vector Search supports the $vectorSearch filter option for the following MQL operators:

Type
MQL operator

Equality

Range

In set

Existence

Logical

  • MongoDB Vector Search supports the short form of $eq. In the short form, you don't need to specify $eq in the query.

    For example, consider the following filter with $eq:

    "filter": { "_id": { "$eq": ObjectId("5a9427648b0beebeb69537a5") }

    This is equivalent to the following filter, which uses the short form of $eq:

    "filter": { "_id": ObjectId("5a9427648b0beebeb69537a5") }
  • You can use the $and MQL operator to specify an array of filters in a single query.

    For example, consider the following pre-filter for documents with a genres field equal to Action and a year field with the value 1999, 2000, or 2001:

    "filter": {
    "$and": [
    { "genres": "Action" },
    { "year": { "$in": [ 1999, 2000, 2001 ] } }
    ]
    }
  • For advanced filtering capabilities such as fuzzy search, phrase matching, location filtering, and other analyzed text, use the vectorSearch operator in a $search stage.

Before you run these examples, perform the following actions:

  • Add the dataset to your cluster.

  • Create MongoDB Vector Search indexes for the collection. For instructions, see the Create a MongoDB Vector Search Index procedure and copy the configurations for the basic or filter examples in your desired language.

Note

If you use mongosh, pasting the queryVector from the sample code into your terminal might take a while depending on your machine.

Back

Index Reference

Earn a Skill Badge

Master "Vector Search Fundamentals" for free!

Learn more

On this page