Atlas Search $search syntax for performing no filter

I’m new to Atlas Search and I’m trying to build a pipeline stage with $search. Based on the usecase that I’m working with, I am searching through a collection that has an Atlas Search index defined, and there will sometimes be a query to match some field with some input (the details aren’t necessary here); however, when there is no input for the query, I want the search query to just return everything in the collection without any filtering/matching. What syntax can I use for $search that has the effect of basically doing nothing and returning the whole collection (similar to .find({} on a MongoDB collection)?

The access pattern you have described is not totally unheard of, and you can do what what you described with Atlas Search. Write a method or function that executes a search compound query if there is a filter condition, and if not, run a wildcard query across a wildcard path.

That second query that filters nothing is:

{
  $search: {
    "index": <index name>, // optional, defaults to "default"
    "wildcard": {
      "query": "*",
      "path": {"wildcard": "*"},
      "allowAnalyzedField": true
    }
  }
}
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.