$count and $sort inside $search

I believe having the $count and $sort inside $search has been asked by lot of people.
Wanted to know how others are doing it until the feature is provided.
I am observing high memory usage by doing $count and $sort after the $search. If there are 2 different processes mongot for $search and mongod for $count and $sort what is causing this high memory?
Any ideas?


Hi again @Supriya_Bansal!

Customers are using the near operator to sort numeric results today. Most search use cases want to be sorted by relevance, so near influences relevance based on numeric order. However, if you want an absolute sort, you can boost the near clause with an arbitrarily high number like:

  $search: {
    "compound": {
      "must": [
         "text": {
                  "query": "Adam",
                  "path": "A"
         "range": {
                  "path": "B"
         "near": {
                  "path": "B",
                  "pivot": 1,
                  "score": { "boost": { "value": 999} }

We are releasing a search optimized count operator in a couple months, and search optimized sort soon after.

1 Like

Thanks @Marcus!
I am using the “wildcard” operator and our clients want the results to be sorted alphabetically.
Would you have an example?

We don’t support that today using the $search operator alone. It is coming soon.

Thank you @Marcus for the update!!