【问题标题】:How to fetch counts from a query from a mongodb collection?如何从 mongodb 集合的查询中获取计数?
【发布时间】:2021-04-24 11:44:45
【问题描述】:

我想创建一种过滤体验,我从请求参数中获取过滤条件,并使用 mongo 聚合框架 过滤数据。请建议我一个合适的方法。

让数据的集合像

        {
            "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"
        },

让查询的数据为 _user 等于 5f9b78bf1130ca88e2e1d, material 等于 silver, mixed 的数组, type 等于 珠子、珠宝 的数组 & shape 等于 round

的数组

我的查询应该返回每个过滤器类别(用户、材料、形状、类型)中的后续计数。

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
    },
  ]
]

【问题讨论】:

    标签: mongodb aggregation-framework filtering dsa


    【解决方案1】:

    您可以使用$facet 对传入数据进行分类

    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
                }
              }
            }
          ]
        }
      }
    ])
    

    工作Mongo playground

    【讨论】:

    • 谢谢!!对数据进行分类很好,但是如何根据查询对其进行过滤。让查询为,_user = 5f9b78bf1130ca88e2e1d material = [silver, mixed] type = [beads, Jewels] shape = [round] 过滤数据 => 分类 => 响应
    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多