How to use $search in $facet Stage?

I’m doing searches over my collection.

                '$search': {
                    'compound': {
                        'must': [
                                'text': {
                                    'query': 'bananas',
                                    'path': 'description'
            }, { '$limit': 10}

Including the Searched results, I also need to get the total no. of matched docs.
For doing this . I’m using $count
I’ve to run two $search aggregation separately in my Backend.

1st aggregation returns Searched Results only in an array format
2nd aggregation returns the total count of the document

Then I combine those two output to a single array manually.
Similar to:
Promise.all( aggegration1, aggegration2)

Isn’t that any way to combine those two aggregation in single stage with $facet ?
which can return the both searched results and count ?

Thanks :wink:

Hi there! Great question. Here is a tutorial laying out how to do use facet with search and here are some more examples specific to counting. Let me know if you have followup questions!

1 Like

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