Mongodb 用于facets导航的Mongo聚合查询

Mongodb 用于facets导航的Mongo聚合查询,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我收集了产品,其中包含如下文档(产品)(仅限相关代码): 我只想向用户提供可用的选择,作为分面搜索,比如选项的名称和可用选项的数量 颜色: red 2 blue 2 green 1 yellow 1 尺寸: S 2 M 1 XL 2 XXL 1 如果必须在SQL中执行此操作,我可能必须为每个方面组运行一个查询 SELECT colors, count(*) AS number FROM products GROUP BY colors 然后是第二个查询 SELECT size, count

我收集了
产品
,其中包含如下文档(产品)(仅限相关代码):

我只想向用户提供可用的选择,作为分面搜索,比如选项的名称和可用选项的数量

颜色:

red 2
blue 2
green 1
yellow 1
尺寸:

S 2
M 1
XL 2
XXL 1
如果必须在SQL中执行此操作,我可能必须为每个方面组运行一个查询

SELECT colors, count(*) AS number FROM products GROUP BY colors
然后是第二个查询

SELECT size, count(*) AS number FROM test GROUP BY sizes
如果有人选择“红色”,我可能需要在每个查询中添加“WHERE”子句:

有没有比我更有经验的人能帮我在MongoDB中举个例子?我是否必须运行两个查询,或者是否有我缺少的东西可以在一个查询中完成?我想我会有很多产品。谢谢你的帮助。特别是如果有什么技巧可以加速的话。我想从一开始就做对,因此提出了这个问题。谢谢。

美元放松的魔力

当您需要计算数组中的某些内容时,必须首先将其展开。看看这个:

db.products.aggregate([ { $unwind : "$colors" }] )
它为数组的每个项生成一行

展开后,您可以通过管道连接到下一组:

db.products.aggregate([ 
{ $unwind : "$colors" }, 
{$group: { _id : "$colors", total_colors : { $sum : 1} } } 
] )
按照您的意愿命名聚合字段:)也可以根据需要对大小进行分组

db.entry.aggregate([
{$unwind : "$size" },
{$group: { "_id" : "$size", count : { $sum : 1 } } }
]).pretty()
希望这有帮助,您可以根据需要自定义“计数”字段和$unwind定义:

从输入文档解构数组字段以输出每个元素的文档。每个输出文档都是输入文档,数组字段的值由元素替换


分面搜索在MongoDB数据库的3.4版中实现

{ $facet:
   {
      <outputField1>: [ <stage1>, <stage2>, ... ],
      <outputField2>: [ <stage1>, <stage2>, ... ],
      ...

   }
}
{$facet:
{
: [ , ... ],
: [ , ... ],
...
}
}

这是

以下是使用MongoDB聚合进行查询的解决方案。下面是我的虚拟收藏

    {
        "_id" : ObjectId("584b82055855b8ea7ea29d65"),
        "colors" : [ 
            "Red", 
            "Yellow"
        ],
        "size" : [ 
            "S", 
            "M"
        ]
    }
    {
        "_id" : ObjectId("584b82185855b8ea7ea29d66"),
        "colors" : [ 
            "Red", 
            "Orange"
        ],
        "size" : [ 
            "S", 
            "XL"
        ]
    }
.
.
在运行下面的查询之后

    db.getCollection('products').aggregate([
    {$unwind : "$colors"},
    {$group : {
        _id : "$colors",
        "sum": {$sum : 1}
    }},
    {
        $project : {
            _id : 0,
            "color":"$_id",
            "count":"$sum"
        }
    }
])
那么输出是:

{
    "color" : "Green",
    "count" : 2
}
{
    "color" : "Orange",
    "count" : 1
}
{
    "color" : "Yellow",
    "count" : 2
}
{
    "color" : "Red",
    "count" : 2
}
如果您只想查找红色颜色的总和,请在下面查询此值

db.getCollection('products').aggregate([
    {$unwind : "$colors"},
    {$match : {"colors":"Red"}},
    {$group : {
        _id : "$colors",
        "sum": {$sum : 1}
    }},
    {
        $project : {
            _id : 0,
            "color":"$_id",
            "count":"$sum"
        }
    }
])
上述查询的输出为:

{
    "color" : "Red",
    "count" : 2
}

您使用的是哪个版本的MongoDB?
db.getCollection('products').aggregate([
    {$unwind : "$colors"},
    {$match : {"colors":"Red"}},
    {$group : {
        _id : "$colors",
        "sum": {$sum : 1}
    }},
    {
        $project : {
            _id : 0,
            "color":"$_id",
            "count":"$sum"
        }
    }
])
{
    "color" : "Red",
    "count" : 2
}