【问题标题】:Mongodb need count for specific member id per dayMongodb每天需要计算特定成员ID
【发布时间】:2015-11-15 21:14:56
【问题描述】:

集合名称:活动
我需要的是活动计数
“memberId”= 123
其中活动“类型”=“xxx”
每天
在“2015 年 11 月 1 日”和“2015 年 11 月 15 日”之间 // 从日期到日期范围
预期输出:
[
{日期:“2015-02-22”,计数:10},
{日期:“2015-02-22”,计数:5},
]

我不知道如何在日期和特定成员 ID 之间执行聚合

我所在的位置与解决方案相去甚远:

db.activity.aggregate(
{ $project: {
    date: {
        years: {$year: '$dateInserted'},
        months: {$month: '$dateInserted'},
        days: {$dayOfMonth: '$dateInserted'},

    },
    memberId: '$memberId'
}},
{ $group: {

    _id: { memberId: '$memberId', date: '$date' },
    number: { $sum: 1}
}})

【问题讨论】:

  • 您能否edit您的问题包括一些示例文档和预期输出?
  • 试图在 mongolab 上创建一个虚拟数据库,但 mongolab 面临集合结构为 {memberId:1,item:"text",dateInserted:"2015-02-22 02:46:31.225Z 的问题"},预期输出是值数组,如每天的活动,如 [{date:"2015-02-22 02:46:31.225Z",count:10}] 用于图表

标签: mongodb mongoose mongodb-query


【解决方案1】:
db.activity.aggregate([ 
 {$match : { memberId : "xxx",item:"xyz",dateInserted: {$gte: ISODate("2013-01-01T00:00:00.0Z"), $lt: ISODate("2016-02-01T00:00:00.0Z")}}},
 {$project: {day: {day: {$dayOfMonth: '$dateInserted'}, month: {$month: '$dateInserted'}, year: {$year: '$dateInserted'}}}},
 {$group: { _id: { day: '$day' }, count: { $sum: 1} }},{ $sort:{_id:1}}
 ]);

试试看

【讨论】:

    【解决方案2】:

    您可能需要在$project 之前添加$match 阶段:

        $match: 
        {dateInserted:{{$gte:new Date("2015-01-11T00:00:00 -02:00"),{$lte:new Date("2015-01-15T23:59:59 -02:00")}}},
        {memberId:123},
        {type:"xxx"} 
    

    【讨论】:

    • 根目录。不适合我,我没有看到成员 ID 和活动类型问题已解决,screencast.com/t/1T2BObQgDedz
    • 这个怎么样: $match: {dateInserted:{ {$gte:new Date("2015-01-11T00:00:00 -02:00")}, {$lte:new Date ("2015-01-15T23:59:59 -02:00")} }},{memberId:123},{type:"xxx"}
    • 'db.activity.aggregate( [ { $match : { memberId : "xxx",item:"text",dateInserted: {$gte:new Date("2015-11-01T00:00 :00 -02:00"), $lte:new Date("2015-11-15T23:59:59 -02:00")}}} ]);'不工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多