liveBook · Manning

Prefiltering

As explained in MongoDB 8.0 in Action, Third Edition

A prefilter is a predicate applied before vector similarity search to restrict the candidate documents.

Prefiltering applies criteria before vector similarity search, narrowing the candidate documents and potentially improving query performance. In MongoDB Atlas Vector Search, filters are supplied through the $vectorSearch stage. section 12.3.2

Use prefiltering effectively by preparing the index, choosing supported predicates, and checking pipeline constraints.

  • Index every field used for filtering by declaring it with the filter type in the vectorSearch index definition. section 12.3.2
  • Filter BSON boolean, date, objectId, string, and numeric values with operators including $eq, $ne, $in, $nin, comparison operators, $and, $or, and $nor. section 12.3.2
  • Respect aggregation limitations: only $and and $or are supported as aggregation-pipeline operators, and objectId fields do not support range comparisons. section 12.3.2
  • Keep $vectorSearch out of view definitions, $lookup and $unionWith subpipelines, and $facet stages. section 12.3.2

Filtering a movie similarity search

A $vectorSearch query can use $or to select movies whose genre is Action or whose runtime is below 120 minutes. The queryVector searches the plot_embedding field, while numCandidates and limit control candidate and result counts. section 12.3.2

Listing 12.5 Prefiltering data with the $vectorSearch pipeline stage
db.embedded_movies.aggregate([
  {
    // The $vectorSearch stage performs a search based on vector similarity
    "$vectorSearch": {
      "index": "MongoDB-in-ActionVectorSearchIndex",
    // #1
      "path": "plot_embedding", // #2
      "queryVector": [
      -0.0016261312, -0.028070757, -0.011342932, -0.012775794,
      -0.0027440966,0.008683807, -0.02575152, -0.02020668, 0.010283281,
      -0.0041719596,0.021392956, 0.028657231, -0.006634482,
      0.01853878, 0.0038187427,0.02959257, 0.01451522, 0.016061379,
      0.000028442,0.008943722, 0.01627464, 0.024311995,
      -0.025911469, 0.00022596726,-0.008863748, 0.008823762,
      -0.034921836, 0.007910728,0.01515501,0.019300312,0.010749794,
      0.0075308536,0.018287312,0.016567878,0.012869096,0.015528221,
      0.0078107617,0.003415542,0.025911469,0.00022596726,
      0.0088637480.011156326,0.013522214,0.020646530.0121601,
      0.011596181,0.017247654,0.0005939711,-0.026977783,-0.003942035,
      0.007890735,0.009690142,0.02020668,0.0012962399,0.025911469,
      0.00022596726,0.0088637480.003415542,0.00024970944,0.012775794,
      0.0114762215,0.013422247,0.010429899,0.03686786,0.006717788,
      0.027484283,0.011556195,0.036068123,0.013915418,0.02020668,
      0.001292399,0.02020668,0.001292399,0.003415542,0.003415542,
      0.0016327957,0.0151016945,0.020473259,0.004671795,0.012555866,
      0.0209531,0.01982014,0.024485271,0.0105431955,0.005178295,
      0.033162415,0.013795458,0.007150979,0.010243294,0.005644808,
      0.0045618312,0.0024725192,0.004305249,0.008197301,0.0014203656,
      0.0018460588,0.005015015, 0.02020668,-0.0012962399,-0.003415542
      ], // The shortened query embedding
      "filter": {
        "$or": [
          { genres: "Action" },//  #3
          { runtime: { "$lt": 120 } } // #4
        ]
      },
      "numCandidates": 150,//  #5
      "exact": false, // #6
      "limit": 5 // #7
    }
  },
  {
    // The $project stage specifies which fields to include in the output
    "$project": {
      "_id": 0, // #8
      "plot": 1, // #9
      "title": 1, // #10
      "score": { $meta: "vectorSearchScore" } // #11
    }
  }
])

Inspect the aggregation example to see where the filter, vector field path, query vector, and search controls belong in $vectorSearch. section 12.3.2

from pymongo import MongoClient

def vector_search():
    # MongoDB connection string
    uri = 'your_mongodb_connection_string'
    # Create a new MongoClient
    client = MongoClient(uri)

    try:
        # Connect to the MongoDB server
        client = MongoClient(uri)
        # Select the database and collection
        database = client['your_database_name']
        collection = database['your_collection_name']

        # Define the pre-filters and vector search query
        query = {
            "$and": [
                {"category": "electronics"},  # Example pre-filter
                {"price": {"$lt": 500}},  # Another example pre-filter
                {
                    "$vectorSearch": {
                        "queryVector": [/* your query vector here */],
                        "numResults": 10,
                        "path": "your_vector_field_name"
                    }
                }
            ]
        }

        try:
            # Execute the vector search query
            ➥ # and convert the results to a list
            results = list(collection.find(query))
            # Print the results
            for result in results:
                print(result)
        except Exception as query_error:
            # Handle errors that occur during the query execution
            print('Error executing vector search query:', query_error)
    except Exception as connection_error:
        # Handle errors that occur while connecting to MongoDB
        print('Error connecting to MongoDB:', connection_error)
    finally:
        try:
            # Ensure the MongoDB client is closed
            client.close()
        except Exception as close_error:
            # Handle errors that occur while closing the MongoDB connection
            print('Error closing MongoDB connection:', close_error)

# Call the vector_search function
vector_search()

The Python pattern combines category and price prefilters with $vectorSearch, executes the query through pymongo, prints results, and includes connection error handling. section 12.4.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains index preparation, supported predicates, pipeline restrictions, and MongoDB driver implementations.
Examples
Uses movie genre and runtime filters, plus electronics and price filters in MongoDB, Python, and Ruby examples.
What it assumes
Requires a MongoDB collection, vector field, query vector, and filter fields configured in a vector-search index.
What you can do after
Apply it via $vectorSearch and MongoDB drivers after indexing filter fields and composing supported predicates.
How it connects
Connects vector similarity search with MongoDB Query API predicates, aggregation pipelines, and vector-search indexes.

Also explained in

Compare how 2 books explain prefiltering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.