Add pre-filter support to cosmosSearch vector search operator
What should be done?
Add pre-filter support to the cosmosSearch operator so that vector searches can be scoped to a subset of documents before the ANN (approximate nearest neighbor) search is performed.
Motivation
The current $search / cosmosSearch pipeline performs ANN search across the entire collection. Many real-world applications (multi-tenant RAG systems, document stores with access control, domain-partitioned knowledge bases) require narrowing the candidate set before computing vector similarity, not as a post-filter $match stage.
A post-filter applied after k results are returned is not equivalent: it silently reduces the returned set below k and, more critically, it cannot enforce security constraints at the database level (a tenant's private documents may be ranked and scored even if they are discarded afterward).
Proposed syntax
Aligned with the MongoDB Atlas $vectorSearch operator's filter parameter, which accepts a standard MQL query expression:
{
$search: {
cosmosSearch: {
vector: [ /* query vector */ ],
path: "embedding",
k: 10,
efSearch: 40,
filter: {
"metadata.tags": { $all: ["engineering", "internal"] }
}
}
}
}The filter field should accept any MQL expression (equality, $in, $all, $elemMatch, range queries) evaluated against indexed fields before the ANN search is executed.
Real-world use case
A self-hosted RAG knowledge base using FerretDB stores document chunks in a textSegments collection. Each chunk carries a metadata.tags array and a metadata.pathPrefixes array. At query time, the system must:
- Restrict the candidate pool to chunks the requesting user is authorized to read (tag intersection with JWT claims).
- Optionally restrict by directory path for scoped retrieval.
- Return the top-k semantically similar chunks from that restricted pool.
Without pre-filter support, step 1 cannot be enforced at the database layer, making it impossible to use FerretDB as a drop-in replacement for MongoDB Atlas $vectorSearch in access-controlled deployments.
Why this matters for FerretDB adoption
MongoDB Atlas $vectorSearch has had filter support since 2023. It is a common requirement in production RAG architectures. Without it, FerretDB's vector search is limited to single-tenant or public-access use cases, which significantly narrows the addressable workload.
References
- MongoDB Atlas $vectorSearch filter docs: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
- FerretDB vector search guide: https://docs.ferretdb.io/guides/vector-search/
Source: FerretDB/FerretDB