0

I want to create a filter experience where I take filter criteria from request params and do the filtering of data using the mongo aggregation framework. Please suggest me a suitable approach.

Let the collection of data be like

        {
            "material": "silver",
            "type": "beads",
            "shape": "round",
            "_user": "5f9b78bf1130ca88e2e1d"
        },
        {
            "material": "mixed",
            "type": "jewels",
            "shape": "round",
            "_user": "5f9b78bf1130ca88e2e1d"
        },
        {
            "material": "gold",
            "type": "jewels",
            "shape": "other",
            "_user": "5fda2b717112437f9c06"
        },
        {
            "material": "silver",
            "type": "jewels",
            "shape": "other",
            "_user": "5f9b78bf1130ca88e2e1d"
        },
        {
            "material": "mixed",
            "type": "jewels",
            "shape": "other",
            "_user": "5fda2b717112437f9c706"
        },

Let the queried data be _user equals 5f9b78bf1130ca88e2e1d, material equals an array of silver, mixed, type equals an array of beads, jewels & shape equals an array of round

My query should return subsequent counts in each filter category(user, material, shape, type).

data = {
  categorizedByUser: [
    {
      _id: '5f9b78bf1130ca88e2e1d',
      count: 2
    },
    {
      _id: '5fda2b717112437f9c06',
      count: 0
    },
  ],
  categorizedByMaterial: [
    {
      _id: 'silver',
      count: 1
    },
    {
      _id: 'mixed',
      count: 1
    },
    {
      _id: 'gold',
      count: 0
    },
  ],
  categorizedByShape: [
    {
      _id: 'round',
      count: 2
    },
    {
      _id: 'other',
      count: 0
    },
  ]
  categorizedByType: [
    {
      _id: 'beads',
      count: 1
    },
    {
      _id: 'jewels',
      count: 1
    },
  ]
]

1 Answer 1

1

You can use $facet to categorize incoming data

db.collection.aggregate([
  {
    $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
            }
          }
        }
      ]
    }
  }
])

Working Mongo playground

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks !! Categorize the data is fine but how can I filter it based on the query. Let the query be, _user = 5f9b78bf1130ca88e2e1d material = [silver, mixed] type = [beads, jewels] shape = [round] Filter data => Categorization => Response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.