Mongodb 如何从以前的$group中提取多个$bucket集合

Mongodb 如何从以前的$group中提取多个$bucket集合,mongodb,aggregation-framework,bucket,Mongodb,Aggregation Framework,Bucket,我想在$bucket函数中单独处理$group的结果 我的数据: { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "type": 1} { "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "type": 1} { "_id" : 3, "title" : "Dancer", "a

我想在$bucket函数中单独处理$group的结果

我的数据:

{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "type": 1}
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "type": 1}
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, "type": 1}
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", "type": 1}
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, "type": 3}
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, "type": 3}
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, "type": 2}
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, "type": 2}
我需要做的是:

  • 按类型分组->返回3组
  • 按年份范围划分,每组在1年内返回
  • 获取2中每个桶的统计信息,1中每个组的统计信息
  • 我希望在一个查询中处理所有内容,而无需多次运行“匹配”以按类型筛选

    预期产出:

    {
        "1": {
            1900: [{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "type": 1}],
            1925: [{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "type": 1}, { "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, "type": 1}],
            "other": [{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", "type": 1}]
        }
        "2": {
            1900: [{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, "type": 2}],
            1925: [],
            "other": [{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, "type": 2}]
        },
        "3": {
            1900: [{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, "type": 3}],
            1925: [{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, "type": 3}],
            "other": []
        }
    }
    

    我知道如何执行一个bucket、一个组和一个match,但我不知道如何在不重复match查询的情况下将其组合到同一聚合管道中以获得预期结果(例如,使用$facet是可能的)。

    使用此管道可以实现类似的输出:

    db.artwork.aggregate([
        { $bucket: {
            groupBy: "$year",
            boundaries: [1900, 1925, 1950],
            default: "Other",
            output: { titles: { $push: "$$ROOT" }}
        }},
        { $unwind: "$titles" },
        { $group: {
            _id: { type: "$titles.type", year: "$_id" },
            titles: { $push: "$titles" }
        }},
        { $project: {
            _id: 0,
            type: "$_id.type",
            year: "$_id.year",
            titles: 1
        }},
        { $group: {
            _id: { type: "$type" },
            buckets: { $push: "$$ROOT" }
        }},
        { $project: {
            _id: 0,
            type: "$_id.type",
            buckets: { year: 1, titles: 1 }
        }}
    ])
    
    其中:

    {
        "type" : 1,
        "buckets" : [
            {
                "year" : 1900,
                "titles" : [ { "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "type" : 1 } ]
            },
            {
                "year" : 1925,
                "titles" : [
                    { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "type" : 1 },
                    { "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, "type" : 1}
                ]
            },
            {
                "year" : "Other",
                "titles" : [ { "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", "type" : 1 } ]
            }
        ]
    },
    
    {
        "type" : 2,
        "buckets" : [
            {
                "year" : 1900,
                "titles" : [ { "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, "type" : 2 } ]
            },
            {
                "year" : "Other",
                "titles" : [ { "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, "type" : 2 } ]
            }
        ]
    },
    
    {
        "type" : 3,
        "buckets" : [
            {
                "year" : 1900,
                "titles" : [ { "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, "type" : 3 } ]
            },
            {
                "year" : 1925,
                "titles" : [ { "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, "type" : 3 } ]
            }
        ]
    }
    
    项目阶段主要是格式化数据,但这里重要的是首先获取所有用于分组项目的数据(每次失误的分类是我们还没有的唯一输出),用$unwind分隔每个项目,并使用$group阶段嵌套阵列

    您想要的输出可以通过$cond操作符实现,但它不是动态的,因为您必须显式地写入字段。更多关于这方面的信息可以在这篇文章中找到