【问题标题】:MongoDB nested group?MongoDB嵌套组?
【发布时间】:2014-10-29 05:52:42
【问题描述】:

我正在尝试在 mongodb 中实现嵌套组查询,但在尝试添加外部组时遇到了困难。给定以下(简化的)数据文档:

{
  "timestamp" : ISODate(),
  "category" : "movies",
  "term" : "my movie"
}

我正在尝试获得所有类别的列表,并且在类别中应该有最多的术语。我希望我的输出是这样的:

[
 { category: "movies", 
   terms: [ { term: "movie 1", total: 5000 }, { term: "movie 2", total: 200 } ... ]
 },
 { category: "sports", 
   terms: [ { term: "football 1", total: 4000 }, { term: "tennis 2", total: 250 } ... ]
 },
]

我的“内组”如下图,将获得所有类别的前5名:

db.collection.aggregate([
    { $match : { "timestamp": { $gt: ISODate("2014-08-27") } } },
    { $group : { _id :  "$term", total : { $sum : 1 } } },
    { $sort : { total : -1 } },
    { $limit: 5 }
]);

// Outputs:
{ "_id" : "movie 1", "total" : 943 }
{ "_id" : "movie 2", "total" : 752 }

我将如何实施“外部小组”?

此外,有时上述聚合]ion 会返回空值(并非所有文档都有术语值)。如何忽略空值?

提前致谢

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    在这种情况下,您将需要两个组。第一组生成一个文档流,每个术语和类别一个文档:

     { $group : { 
          _id :  { 
            category: "$category",
            term: "$term",
          },
          total: { $sum : 1 } 
       }
     }
    

    然后第二组将所有具有相同术语的文档合并为一个,使用$push 运算符将类别合并到一个数组中:

     { $group : { 
          _id :  "$_id.category",
          terms: { 
              $push: { 
                  term:"$_id.term",
                  total:"$total"
              }
          }
       }
     }
    

    【讨论】:

    • 谢谢!这解决了我的问题,我在两个组之间添加了{ $sort : { total : -1 } } 以使整个最分组的术语位于顶部,但我无法弄清楚如何将术语限制为每个类别 5 个。在组之间添加 { $limit: 6 } 不起作用。 (我正在尝试对每个类别查询进行前 5 个术语)。谢谢!
    • 也帮助了我!魔法!
    【解决方案2】:

    查询:

        db.getCollection('orders').aggregate([
        {$match:{
            tipo: {$regex:"[A-Z]+"}
            }
        },
        {$group:
            { 
                _id:{
                    codigo:"1",
                    tipo:"$tipo",
                },
                total:{$sum:1}
            }
        },
        {$group:
            {
                _id:"$_id.codigo",
                tipos:
                {
                    $push:
                    {
                        tipo:"$_id.tipo",
                        total:"$total"
                    }
                },
                totalGeneral:{$sum:"$total"}
    
            }
    
        }
    
    
    ]);
    

    回复:

    {
    "_id" : "1",
    "tipos" : [ 
        {
            "tipo" : "TIPO_01",
            "total" : 13.0
        }, 
        {
            "tipo" : "TIPO_02",
            "total" : 2479.0
        }, 
        {
            "tipo" : "TIPO_03",
            "total" : 12445.0
        }, 
        {
            "tipo" : "TIPO_04",
            "total" : 12445.0
        }, 
        {
            "tipo" : "TIPO_05",
            "total" : 21.0
        }, 
        {
            "tipo" : "TIPO_06",
            "total" : 21590.0
        }, 
        {
            "tipo" : "TIPO_07",
            "total" : 1065.0
        }, 
        {
            "tipo" : "TIPO_08",
            "total" : 562.0
        }
    ],
    "totalGeneral" : 50620.0
    

    }

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2013-06-22
      • 2015-06-20
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多