mongodb:如何从对象数组构建构面

发布于 2025-01-09 08:19:45 字数 1524 浏览 4 评论 0原文

从每个产品具有不同属性的对象数组中创建构面的最佳方法是什么?

示例数据:

[
  {
    "key": 1,
    "title": "Product A",
    "attrs": [
      {
        "keyasd": "size",
        "value": "small"
      },
      {
        "kedasy": "color",
        "value": "red"
      }
    ]
  },
  {
    "key": 2,
    "title": "Product B",
    "attrs": [
      {
        "key": "size",
        "value": "large"
      },
      {
        "key": "color",
        "value": "blue"
      }
    ]
  },
  {
    "key": 3,
    "title": "Product C",
    "attrs": [
      {
        "key": "resolution",
        "value": "8K"
      },
      {
        "key": "refresh rate",
        "value": "60 Hz"
      },
      
    ]
  }
]

我想要得到的结果是这样的:

[
    {
        "_id": {
            "key": "size",
            "values" : [
                {"title": "small", "count": 1},
                {"title": "large", "count": 1}
            ]
        }
    },
    {
        "_id": {
            "key": "color",
            "values" : [
                {"title": "red", "count": 1},
                {"title": "blue", "count": 1}
            ]
        }
    },
    {
        "_id": {
            "key": "resolution",
            "values" : [
                {"title": "8K", "count": 1}
            ]
        }
    },
    {
        "_id": {
            "key": "refresh rate",
            "values" : [
                {"title": "60 Hz", "count": 1}
            ]
        }
    }
]

我不知道我输入的结果是否可能,但我需要以某种方式构建它,即使它是单独的每个属性类型的每个方面产品可以有

What would be the best way to create facets from an array of objects with different attributes for each product.

Example Data:

[
  {
    "key": 1,
    "title": "Product A",
    "attrs": [
      {
        "keyasd": "size",
        "value": "small"
      },
      {
        "kedasy": "color",
        "value": "red"
      }
    ]
  },
  {
    "key": 2,
    "title": "Product B",
    "attrs": [
      {
        "key": "size",
        "value": "large"
      },
      {
        "key": "color",
        "value": "blue"
      }
    ]
  },
  {
    "key": 3,
    "title": "Product C",
    "attrs": [
      {
        "key": "resolution",
        "value": "8K"
      },
      {
        "key": "refresh rate",
        "value": "60 Hz"
      },
      
    ]
  }
]

The result I would like to get would be something like this:

[
    {
        "_id": {
            "key": "size",
            "values" : [
                {"title": "small", "count": 1},
                {"title": "large", "count": 1}
            ]
        }
    },
    {
        "_id": {
            "key": "color",
            "values" : [
                {"title": "red", "count": 1},
                {"title": "blue", "count": 1}
            ]
        }
    },
    {
        "_id": {
            "key": "resolution",
            "values" : [
                {"title": "8K", "count": 1}
            ]
        }
    },
    {
        "_id": {
            "key": "refresh rate",
            "values" : [
                {"title": "60 Hz", "count": 1}
            ]
        }
    }
]

I don't know if the result I put is possible, but I need to somehow build it, even if it's individually each facet for each type of attribute that a product can have

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-01-16 08:19:45
db.collection.aggregate([
  {
    "$unwind": "$attrs"
  },
  {
    "$group": {
      "_id": {
        k: "$attrs.key",
        v: "$attrs.value"
      },
      "count": { "$sum": 1 }
    }
  },
  {
    "$group": {
      "_id": "$_id.k",
      "values": {
        "$push": {
          "title": "$_id.v",
          "count": "$count"
        }
      }
    }
  },
  {
    "$project": {
      "_id": {
        key: "$_id",
        values: "$values"
      }
    }
  }
])

mongoplayground

db.collection.aggregate([
  {
    "$unwind": "$attrs"
  },
  {
    "$group": {
      "_id": {
        k: "$attrs.key",
        v: "$attrs.value"
      },
      "count": { "$sum": 1 }
    }
  },
  {
    "$group": {
      "_id": "$_id.k",
      "values": {
        "$push": {
          "title": "$_id.v",
          "count": "$count"
        }
      }
    }
  },
  {
    "$project": {
      "_id": {
        key: "$_id",
        values: "$values"
      }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文