MongoDB nested group?

2020-05-16 05:33发布

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 } ... ]
 },
]

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?

thanks in advance

2条回答
乱世女痞
2楼-- · 2020-05-16 05:59

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 } 
   }
 }

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"
          }
      }
   }
 }
查看更多
来,给爷笑一个
3楼-- · 2020-05-16 06:06

Query:

    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"}

        }

    }


]);

Response:

{
"_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

}

查看更多
登录 后发表回答