MongoDB嵌套组? [英] MongoDB nested group?

查看:76
本文介绍了MongoDB嵌套组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在mongodb中实现嵌套的组查询,并且在尝试添加外部组时遇到麻烦.给出以下(简化的)数据文档:

I'm trying to implement a nested group query in mongodb and I'm getting stuck trying to add the outer group by. Given the below (simplified) data document:

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

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

I'm trying to achieve a list of all categories and within the categories there should be the top number of terms. I would like my output something like this:

[
 { 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名:

My 'inner group' is as shown below, and will get the top 5 for all categories:

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 }

我将如何实施外部小组"?

How would I go about implementing the 'outer group'?

此外,有时上述汇总返回一个空值(并非所有文档都具有术语值).如何忽略空值?

Additionally sometimes the above aggregate]ion returns a null value (not all documents have a term value). How do I go about ignoring the null values?

预先感谢

推荐答案

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

You will need two groups in this case. The first group generates a stream of documents with one document per term and category:

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

第二小组随后将使用 $ push 运算符,将类别合并到一个数组中:

A second group will then merge all documents with the same term into one, using the $push operator to merge the categories into an array:

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

这篇关于MongoDB嵌套组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆