Handling regex matching in vectorSearch's filter

Hi all, what is the correct way to express a regex filter in the filter section of vectorSearch? I wanted to add a filter like “chunkText”: { $regex: ‘cashflow’, $options: ‘i’ }, but resulted in the error: ‘Match expression is not supported for $vectorSearch’.

Appreciate any guidance on this. Thank you!

  const cursor = collection.aggregate([ 
    {
      "$vectorSearch": {
        "index": "default",
        "queryVector": embedding,
        "path": "docVector",
        "numCandidates": 100,
        "limit": 30,
        "filter": {
          "$and": [
 	     "docType": "office",
             "chunkText": { $regex: 'cashflow', $options: 'i' }
	  ]
        },
      },
    }
  ]);

$vectorSearch filter only supports the data types and comparison operators as provided in the documentation

The regex based is not supported with $vectorSearch. But you can use regex based filtering in ‘Atlas Search’ in a filter along with the knnBeta operator to get the desired outcome - https://www.mongodb.com/docs/atlas/atlas-search/knn-beta/

1 Like

Thank you for your reply, Prakul. Do you know if $vectorSearch will support regex filters in upcoming releases? Thanks.

We don’t have a public roadmap to share at this point. But we are definitely looking at such features.

1 Like

+1 for the regex.

P.S. For me, a simple “isin” would suffice. I just want to pre-filter docs that contain a substring in a field (my field contains a string with comma-separated tags).