MongoDB:基于日期的查询集合

MongoDB:基于日期的查询集合,mongodb,Mongodb,我在MongoDB中有一个这种格式的集合 db.logins.find().pretty() { "cust_id" : "samueal", "created_at" : "2011-03-09 10:31:02.765" } { "cust_id" : "sade", "created_at" : "2011-03-09 10:33:11.157" } { "cust_id" : "sade",

我在MongoDB中有一个这种格式的集合

db.logins.find().pretty()
{
        "cust_id" : "samueal",
        "created_at" : "2011-03-09 10:31:02.765"
}
{
        "cust_id" : "sade",
        "created_at" : "2011-03-09 10:33:11.157"
}
{
        "cust_id" : "sade",
        "created_at" : "2011-03-10 10:33:35.595"
}
{
        "cust_id" : "samueal",
        "created_at" : "2011-03-10 10:39:06.388"
}
{ "_id" : "dsstest" , "date" : "2011-03-09", "value" : 4 }
{ "_id" : "dsstest" , "date" : "2011-03-10", "value" : 14 }
这是我的mapReduce函数

 m = function() { emit(this.cust_id, 1); }


    r = function (k, vals) { var sum = 0; for (var i in vals) { sum += vals[i]; } return sum; }


    q = function() {
        var currentDate = new Date();
        currentDate.setDate(currentDate.getDate()-32);
        var month = (currentDate.getMonth() < 10 ? "0"+ (currentDate.getMonth()+1) : (currentDate.getMonth()+1));
        var date = currentDate.getFullYear() + "-" + month ;
        var patt = new RegExp(date);
        var query = {"created_at":patt};
        return query;
    }


    res = db.logins.mapReduce(m, r, { query : q(), out :  "userLoginCountMonthly" });
但我需要genearte以这种格式输出报告

db.logins.find().pretty()
{
        "cust_id" : "samueal",
        "created_at" : "2011-03-09 10:31:02.765"
}
{
        "cust_id" : "sade",
        "created_at" : "2011-03-09 10:33:11.157"
}
{
        "cust_id" : "sade",
        "created_at" : "2011-03-10 10:33:35.595"
}
{
        "cust_id" : "samueal",
        "created_at" : "2011-03-10 10:39:06.388"
}
{ "_id" : "dsstest" , "date" : "2011-03-09", "value" : 4 }
{ "_id" : "dsstest" , "date" : "2011-03-10", "value" : 14 }
比如说

Name            Date                Logins

sade            2011-03-09       1
sade            2011-03-10       2
samueal      2011-03-09       1
samueal      2011-03-10       1    
有人能帮我怎么做吗

编辑部分

目前我得到的输出是

{ "_id" : "sade", "value" : 2 }
{ "_id" : "samueal", "value" : 2 }
  { "_id" : "dsstest 2011-03-09", "value" : 4 }
    { "_id" : "dsstest 2011-03-10", "value" : 14 }
我可以用这种格式吗

db.logins.find().pretty()
{
        "cust_id" : "samueal",
        "created_at" : "2011-03-09 10:31:02.765"
}
{
        "cust_id" : "sade",
        "created_at" : "2011-03-09 10:33:11.157"
}
{
        "cust_id" : "sade",
        "created_at" : "2011-03-10 10:33:35.595"
}
{
        "cust_id" : "samueal",
        "created_at" : "2011-03-10 10:39:06.388"
}
{ "_id" : "dsstest" , "date" : "2011-03-09", "value" : 4 }
{ "_id" : "dsstest" , "date" : "2011-03-10", "value" : 14 }

您的映射函数不足,因为它不能生成包含日期的键。 我也不太明白为什么在您想要的示例中,
sade
有两次登录。根据你所说的你想要的,你应该需要:

var m = function() {
   var aux = this.created_at.indexOf(' ');
   aux = this.created_at.substring(0,aux);

   // this 'if' block will filter out the entries you don't want
   // to be included in the result.
   if (aux < "2011-11-11") {
      return;
   }
   emit({cust:this.cust_id,day:aux},1);
}
var r = function(k, values) {
  var l = values.length;
  var r = 0;
  var i;
  for (i=0; i<l; i++) {
    r+=values[i];
  }
  return r;
}
最后,要生成报告:

> db.userLoginCountMonthly.find().forEach(function(e){print(e._id.cust +' ' + e._id.day+' '+e.value);})

这将列出每个用户每天的登录量(在您的搜索范围内)。

我正在尝试修改我的查询,以便它返回集合中的所有数据,如果少于昨天,但给出了错误,请查看编辑部分。在数据库中以字符串形式存储日期不是最好的方法,因为它真的会妨碍你做你想做的事。如果您只想在结果中不包括今天,那么最好在最终报告中忽略今天的日期,或者修改映射函数。我用这个更新了我的答案。我不能使用内部查询作为var query={created_at:{$lt:“2011-11-11”}}你刚才不是说它给了你一个错误吗?(顺便说一句,让人们知道错误到底是什么总是很有用的)。有了它,我能够得到所有的数据。你帮了我很多,坦白说你救了我一个晚上