Filter experience

I want to replicate a filter experience on data. I am able to find it on one website but that is on the frontend, I want to create it using the mongo aggregation framework.
URL - https://www.yatra.com/hotels/hotels-in-mumbai
The important thing about this filter is that it returns a count as zero also. In addition to that when the next filter applied the query should do the OR operation within its collection for counts and AND operation with other collections.
Finally, the API should return data in the form of {id: filterName: count} for all the collections.

Hi @Saransh_Exports;

Could you give an example of what you need?? May be I will be able to help you out? I just want to make sure what I understood by going through your question is correct.

Cheers
Shanka

Hi @Saransh_Exports,

It will be great to have a better example. But if you are talking about a facted multi data filter and counting like the provided site has in its “filter” section, you should use $facet and $bucket or $bucketAuto with a $size to calc the array sizes :

https://docs.mongodb.com/manual/reference/operator/aggregation/facet/#example

Please review the example above and let us know if this is what you are looking for.

Thanks
Pavel

Thank you for taking an interest.

I have asked the same question in the below link with one closest example with dummy data.
https://stackoverflow.com/questions/65803976/how-to-fetch-counts-from-a-query-from-a-mongodb-collection.
Keep me updated in case of any query or success.

Hi Pavel,

Thanks for the revert.

$facet would definitely help. Main challenge is to calculate count = zero based on any filter criteria. Also, I am not sure on decide of bucket boundaries as my data have string values. Sharing an example below via stack-overflow.

Hi @Saransh_Exports,

I ve noticed that someone provided count example via the aggregation, so there is no need for bucketing.

However for your queries on filter you just need to filter the data in a previous $match stage before the facet:

 collection.aggregate([ {$match  : { _user : "...", 
material : { $in :   [silver, mixed] }, 
type : { $in : [beads, jewels] },
 shape : "round" },
  {
    $facet: {
      categorizedByUser: [
        {
          $group: {
            _id: "$_user",
            count: {
              $sum: 1
            }
          }
        }
      ],
      categorizedByMaterial: [
        {
          $group: {
            _id: "$material",
            count: {
              $sum: 1
            }
          }
        }
      ],
      categorizedByShape: [
        {
          $group: {
            _id: "$shape",
            count: {
              $sum: 1
            }
          }
        }
      ],
      categorizedByType: [
        {
          $group: {
            _id: "$type",
            count: {
              $sum: 1
            }
          }
        }
      ]
    }
  }
])

If you need or logic use $or in the match.

Thanks
Pavel

It helped, for counting the criteria that won’t match I am matching the query result with original distict types of values present.
Thank you.

1 Like

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