$match stage before or pre-filter in $vectorSearch

Hi all, I am implementing vector search in my project and I would like to ask which approach is better:

  1. Using a $match stage before $vectorSearch
  2. Using filter inside $vectorSearch (pre-filter)

Also I have to use $elemMatch as my filter criteria but it seems like its not supported by $vectorSearch filter
match expression is not supported for $vectorSearch: 2

here is my code so far


 {
    $vectorSearch: {
      index: "vector_index",
      path: "CourseEmbeddings",
      filter: {
        $and: [
          {
            IsActive: true,
            FacilityId: 3,
            "Status.Value": 2,
            $or: [
              {
                Facilities: {
                  $elemMatch: {
                    _id: { $in: [2] }
                  }
                }
              }
            ]
          }
        ]
      },
      queryVector: [
        -0.0072121937, -0.030757688, 0.014948666
      ],
      numCandidates: 150,
      limit: 10
    }
  }

It is a good idea to use a $match stage at the beginning of any aggregation to limit the number of results which should be processed.

Filtering within a $vectorSearch stage is relatively limited, you can only filter by booleans, strings and numbers (not arrays), and the fields that you want to filter by must be defined in the vector search index.

See https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#std-label-vectorSearch-agg-pipeline-filter

Is there an example of this? I am running into:

$vectorSearch is only valid as the first stage in a pipeline

But unfortunately $vectorSearch should be first stage in aggregation

To clarify @Peter_Hubbard’s comment above, you get similar $match semantics on limited data types within the $vectorSearch prefilter today. We are working on expanding this functionality since you can’t use $match before $vectorSearch.

@Ans_Bilal can you please share a sample document for your use case so I can better understand your $elemMatch usage?