每个月当前月份的总注册用户

时间:2019-09-17 14:30:13

标签: angularjs mongodb mongoose mongoid

enter image description here 在这里,日期是注册日期,通过简单的分组,我得到了这样的结果

    [ 
      { date: '2019-09-01', count: 1 },     
      { date: '2019-09-02', count: 3 },     
      { date: '2019-09-04', count: 2 },     
      { date: '2019-09-05', count: 5 },    
     // ... 
    ]

但是我想要每个日期,如果该日期用户没有注册,则显示计数为0

    [ 
      { date: '2019-09-01', count: 1 }, 
      { date: '2019-09-02', count: 3 },     
      { date: '2019-09-03', count: 0 },     
      { date: '2019-09-04', count: 0 },     
    // ... 
    ]

如果用户没有在3个日期和4个日期注册,则显示0个计数。

monthalldate = [ '2019-09-1',  '2019-09-2',  '2019-09-3',  '2019-09-4',  '2019-09-5',  '2019-09-6',  '2019-09-7',  '2019-09-8',  '2019-09-9',
  '2019-09-10',  '2019-09-11',  '2019-09-12',  '2019-09-13',.......,
  '2019-09-30' ]


  User.aggregate([
                { "$group": {
                  "_id": { "$substr": ["$createdOn", 0, 10] },
                  "count": { "$sum": 1 },
                  "time": { "$avg": "$createdOn" },
                }},
                { "$sort": { "_id": 1 } },
                { "$project": { "date": "$_id", "createdOn": "$count" }},
                { "$group": { "_id": null, "data": { "$push": "$$ROOT" }}},
                { "$project": {
                  "data": {
                    "$map": {
                      "input": monthalldate,
                      "in": {
                        "k": "$$this",
                        "v": { "$cond": [{ "$in": ["$$this", "$data.date" ] }, 1, 0 ] }
                      }
                    }
                  }
                }},
                { "$unwind": "$data" },
                { "$group": { "_id": "$data.k", "count": { "$sum": "$data.v" }}}
              ]).exec(function (err, montlysub) { 
                // console.log(montlysub);

              });
    But I got the wrong result

我的用户集合

 { "_id" : ObjectId("5a0d3123f954955f15fe88e5"), "createdOn" : ISODate("2019-11-16T06:33:07.838Z"), "name":"test" },
 { "_id" : ObjectId("5a0d3123f954955f15fe88e6"), "createdOn" : ISODate("2019-11-17T06:33:07.838Z"), "name":"test2" } 

1 个答案:

答案 0 :(得分:0)

export interface ResponseObject { status: number; /* It's OK to exclude things we don't need, but, the status property is going to come in handy when we eventually want to write a catch statement for cases when the call returns an error instead of the data we wanted */ data: { /* you can also split up your interface declarations to make them easier to read; this has the added benefit of allowing us to more easily access the sub-properties of our payload later on */ result: UserObject; /* we're expecting a single UserObject per ResponseObject in this scenario, otherwise we'd use: */ // result: UserObject[]; } } export interface UserObject { /* remember to use the generic type names (lowercase 'string'), not Primitives (Capitalized 'String') in your interface declaration, otherwise you'll get more errors */ id: number; name: string; email?: string; /* the question mark lets you define a property that *may* exist, but may not. Useful if your target API only includes parameters with non-null values in the ResponseObject */ // ...all the other things you want and think will exist } 转换输入文档。如果特定月份没有用户记录,则没有要转换的输入文档,并且该月您将没有任何输出。

解决方法:

  1. 创建一个仅包含月份的集合,然后从该集合中检索所需的日期范围,并将用户文档加入每个月。

  2. 在结果中进行迭代或作为结果呈现之前的后处理步骤,在应用程序中添加缺少的零数据点。