MongoDB聚合查询

时间:2012-10-26 13:14:54

标签: mongodb output aggregation-framework

在MongoDB中我有一个集合:

Statistics
{
    UserID:  int          //User id
    Url: string           //Url
    Clicks: [DateTime]    //A time array    
}

当用户点击网址时,在Clicks数组中添加点击日期的日期。我的问题是如何编写聚合查询,例如获取从[date1]到[date2]的点击次数以及按用户ID分组?如何将文件输出到文件?

谢谢!

1 个答案:

答案 0 :(得分:4)

假设您有这样的数据(请参阅底部如何生成此内容):

{ "_id": ObjectId("508ab0e27bb16229520c9561"), "userid": 0, "url": "", "clickDate": ISODate("20120101T12:01:00Z") }
{ "_id": ObjectId("508ab0e27bb16229520c9562"), "userid": 1, "url": "", "clickDate": ISODate("20120202T12:01:00Z") }
{ "_id": ObjectId("508ab0e27bb16229520c9563"), "userid": 2, "url": "", "clickDate": ISODate("20120303T12:01:00Z") }
{ "_id": ObjectId("508ab0e27bb16229520c9564"), "userid": 3, "url": "", "clickDate": ISODate("20120404T11:01:00Z") }
{ "_id": ObjectId("508ab0e27bb16229520c9565"), "userid": 4, "url": "", "clickDate": ISODate("20120505T11:01:00Z") }

这是聚合函数:

db.test.aggregate( {
                      $match: {
                        clickDate: { $gte: new Date(2012,8,30,12,0,0) }
                      }
                    },
                    {
                      $group: {
                        _id: "$userid",
                        clicks: { $sum: 1 }
                      }
                    }
                 );

确保$match之前有$group。请参阅early filtering

结果:

{
  "result": [
    { "_id": 8,
      "clicks": 1
    },
    { "_id": 7,
      "clicks": 2
    },
    { "_id": 6,
      "clicks": 2
    },
    { "_id": 3,
      "clicks": 2
    },
    { "_id": 2,
      "clicks": 2
    },
    { "_id": 1,
      "clicks": 2
    },
    { "_id": 4,
      "clicks": 2
    },
    { "_id": 0,
      "clicks": 2
    },
    { "_id": 5,
      "clicks": 2
    },
    { "_id": 9,
      "clicks": 1
    }
  ],
  "ok": 1
}

使用此循环生成数据:

// d=days, m=months (for ISODate months start from 0, while days from 1) 
for (var i = 0, d = 1, m = 0, id = 0; i < 100; i++, d++, m++, id++) {
  if (d > 30){
    d=1;
  }
  if (m > 10){
    m=0;
  }
  if (id > 9){
    id=0;
  }
  db.test.insert({userid: id, url:"", clickDate: new Date(2012,m,d,12,1,0)});
}