MongoDB分组由两个字段和格式结果

时间:2016-02-09 10:49:01

标签: mongodb

我使用mongodb $ group来获取结果。(按标签和时间分组))

我想得到这样的结果:

{
    "_id" : { "hashtag" : "Google"  },
    "info" : [{ "date" : { "year" : 2015, "month" : 12, "day" : 24, "hour" : 16 }, "cnt" : 1 }, 
             { "date" : { "year" : 2015, "month" : 12, "day" : 25, "hour" : 15 }, "cnt" : 4 }, 
             { "date" : { "year" : 2015, "month" : 12, "day" : 25, "hour" : 17 }, "cnt" : 4}, ]
}
{
    "_id" : { "hashtag" : "Microsoft"  },
    "info" : [{ "date" : { "year" : 2015, "month" : 12, "day" : 25, "hour" : 11 }, "cnt" : 1 }, 
             { "date" : { "year" : 2015, "month" : 12, "day" : 24, "hour" : 17 }, "cnt" : 1 }, 
             { "date" : { "year" : 2015, "month" : 12, "day" : 21, "hour" : 18 }, "cnt" : 2 }, ]
}

这是我的代码:

var hashtags = ['Microsoft', 'Google'];
db.stream.aggregate([
    {
        '$match': {
             'hashtags': {"$in": hashtags},
            'time': {
                 $gt:new Date('2015-12-20'),
                 $lt:new Date('2015-12-27')
            }
        }
    },
    {'$unwind': '$hashtags'},
    {
        '$match': {
            'hashtags': {"$in": hashtags},
        }
    },
    {
        '$group': {
            '_id': {
                'hashtags': '$hashtags',
                'year': {'$year': '$time'},
                'month': {'$month': '$time'},
                'day': {'$dayOfMonth': '$time'},
                'hour': {'$hour': "$time"}
            },
            'cnt': {'$sum': 1},
        }
    },
    {
     '$project': {
       '_id':0,
       'hashtag': "$_id.hashtags",
       'info':{
         'date': {
            'year': "$_id.year",
            'month': "$_id.month",
            'day': "$_id.day",
            'hour': "$_id.hour",
          },
         'cnt': "$cnt"
       }
      }
   },
    {
        '$group': {
            '_id': {
                'hashtags': '$hashtag',
            },
            'info':{'$push':'$info'}
        }
    },
])
  1. 首先,我使用匹配来减少数据的数量
  2. 请注意,hashtags是一个数组,(例如:[' Microsoft'' Surface'])因此我使用$ unwind来取得标签,只支持Microsoft或Google。
  3. 我的代码太丑陋而且很慢。还有另一种解决方法吗?

2 个答案:

答案 0 :(得分:0)

您需要使用$project运算符。

var hashtags = ['Microsoft', 'Google'];
db.stream.aggregate([
    {
        '$match': {
             'hashtags': {"$in": hashtags},
            'time': {
                 $gt:new Date('2015-12-20'),
                 $lt:new Date('2015-12-27')
            }
        }
    },
    {'$unwind': '$hashtags'},
    {
        '$group': {
            '_id': {
                'hashtags': '$hashtags',
                'year': {'$year': '$time'},
                'month': {'$month': '$time'},
                'day': {'$dayOfMonth': '$time'},
                'hour': {'$hour': "$time"}
            },
            'cnt': {'$sum': 1},
        }
    },
   {
     '$project': {
       '_id': "$_id.hashtags",
       {
         '_id': {
            'year: "$_id.year",
            'month': "$_id.month",
            'day': "$_id.day",
            'hour': "$_id.hour",
          },
         'cnt': "$cnt"
       }
      }
   }
])

答案 1 :(得分:0)

您的对象不是正确的JS对象。

你应该通过使用mongo和hashtags对它们进行分组来保持简单。

var data = db.stream.group(
   {
     key: { hashtags: 1},
     reduce: function ( curr, result ) { },
     initial: { }
   }
)

数据:

    [{ "_id" : { "hashtags" : "Google", "year" : 2015, "month" : 12, "day" : 24, "hour" : 16 }, "cnt" : 1 },
{ "_id" : { "hashtags" : "Google", "year" : 2015, "month" : 12, "day" : 25, "hour" : 15 }, "cnt" : 4 }
,{ "_id" : { "hashtags" : "Google", "year" : 2015, "month" : 12, "day" : 25, "hour" : 17 }, "cnt" : 4 }
,{ "_id" : { "hashtags" : "Microsoft", "year" : 2015, "month" : 12, "day" : 25, "hour" : 11 }, "cnt" : 1 }
,{ "_id" : { "hashtags" : "Microsoft", "year" : 2015, "month" : 12, "day" : 24, "hour" : 17 }, "cnt" : 1 }
,{ "_id" : { "hashtags" : "Microsoft", "year" : 2015, "month" : 12, "day" : 21, "hour" : 18 }, "cnt" : 2 }]

然后使用javascript构建您的对象:

var result = {
        "Microsoft": [],
    "Google": []
};

映射数据以创建2个属性对象:

data.map(function (elem) {
        result[elem._id.hashtags].push(elem);
})

结果:

{
    "Google": [
        { "_id" : { "year" : 2015, "month" : 12, "day" : 24, "hour" : 16 }, "cnt" : 1 },
        { "_id" : { "year" : 2015, "month" : 12, "day" : 25, "hour" : 15 }, "cnt" : 4 },
        { "_id" : { "year" : 2015, "month" : 12, "day" : 25, "hour" : 17 }, "cnt" : 4 }
    ],
    "Microsoft": [
        { "_id" : { "year" : 2015, "month" : 12, "day" : 25, "hour" : 11 }, "cnt" : 1 },
        { "_id" : { "year" : 2015, "month" : 12, "day" : 24, "hour" : 17 }, "cnt" : 1 },
        { "_id" : { "year" : 2015, "month" : 12, "day" : 21, "hour" : 18 }, "cnt" : 2 }
    ]
}

enter image description here

这样您就可以通过执行以下操作来访问您的数据:result.Googleresult.Microsoft