How to get the total number of subscribers from the start date to the current date?

I am very new to MongoDB and mongoose, and I would like to get the total number of subscribers from the start date to the current date of the day. In other words, I need to group all my subscribers in the afternoon, and for each day I need to summarize all my previous subscribers.

Here are some of my docs:

{ "_id" : ObjectId("574cef8ee62502d08f34c075"), "userId" : "a7zd609h54wm5kt", "subscribedAt" : ISODate("2016-05-30T18:22:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.098Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c076"), "userId" : "3q2tvd53zcap5mq", "subscribedAt" : ISODate("2016-05-30T19:52:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.113Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c077"), "userId" : "nvx8mnu1xis5jxt", "subscribedAt" : ISODate("2016-05-28T19:52:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.117Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c078"), "userId" : "l7eedg616r0zbdf", "subscribedAt" : ISODate("2016-05-28T16:28:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.122Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c079"), "userId" : "9r1wl8ao8bls7g7", "subscribedAt" : ISODate("2016-05-28T11:05:05Z"), "isSubscribed" : false, "createdAt" : ISODate("2016-05-31T01:57:34.125Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c07a"), "userId" : "ygwve2e47p7fanl", "subscribedAt" : ISODate("2016-05-29T00:28:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.125Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c07b"), "userId" : "1gxspx9jypwc9tu", "subscribedAt" : ISODate("2016-05-29T19:52:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.127Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c07c"), "userId" : "hvy4xjppos8ch2b", "subscribedAt" : ISODate("2016-05-29T01:36:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.127Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c07d"), "userId" : "bmpql2d7wkl0jnw", "subscribedAt" : ISODate("2016-05-29T21:50:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.127Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c07e"), "userId" : "uir99sy6q3p6i0i", "subscribedAt" : ISODate("2016-05-29T08:22:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.131Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c07f"), "userId" : "qmzn3rz308ku017", "subscribedAt" : ISODate("2016-05-29T22:02:05Z"), "isSubscribed" : false, "createdAt" : ISODate("2016-05-31T01:57:34.132Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c080"), "userId" : "ok46sxaf8urrqtj", "subscribedAt" : ISODate("2016-05-29T07:33:05Z"), "isSubscribed" : false, "createdAt" : ISODate("2016-05-31T01:57:34.132Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c081"), "userId" : "ot4nmxqsn4o98vn", "subscribedAt" : ISODate("2016-05-29T23:52:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.138Z"), "__v" : 0 }
{ "_id" : ObjectId("574cef8ee62502d08f34c082"), "userId" : "2voy5ttkk39i1f0", "subscribedAt" : ISODate("2016-05-30T00:52:05Z"), "isSubscribed" : true, "createdAt" : ISODate("2016-05-31T01:57:34.138Z"), "__v" : 0 }

So, I have 2 subscribers for 2016-05-28, 6 subscribers for 2016-05-29 and 3 subscribers for 2016-05-30,

The result I hope to achieve is something like this.

{
    "results" : [
        {
            "date" : ISODate("2016-05-28T00:00:00Z"),
            "subscribers" : 2
        },
        {
            "date" : ISODate("2016-05-29T00:00:00Z"),
            "subscribers" : 8
        },
        {
            "date" : ISODate("2016-05-30T00:00:00Z"),
           "subscribers" : 11
        },
    ]
}

I tried the Aggregation Framework with the following code:

var express = require('express'),
    router  = express.Router(),
    User    = require('../models/user');

router.get('/', function(req, res, next) {
    User.aggregate([
        {
            $match: { isSubscribed: true, subscribedAt: {$gte: new Date("2016-05-28T00:00:00+01:00"), $lte: new Date()}}
        }, 
        {
            $group: {
                _id: {$dateToString: { format: "%Y-%m-%d", date: "$subscribedAt" }},
                count : { $sum : 1 }
            },
        }, 
        {
            $project: {
                _id             : 0,
                subscribeDate   : "$_id",
                count           : 1
            }
        },
        {
            $sort: {subscribeDate: 1} 
        }
    ], function(err, result){
        if (err) {
            console.log("This is an error find the user!")
        }
        res.render('dashboard', {results: result, title: "Welcome to analytics dashboard!"});
    });

});
module.exports = router;

But this only gives me the total number of subscribers of each day, NOT a summary of all previous subscribers.

{ "count" : 2, "subscribeDate" : "2016-05-28" }
{ "count" : 6, "subscribeDate" : "2016-05-29" }
{ "count" : 3, "subscribeDate" : "2016-05-30" }

mapReduce, , :

var express = require('express'),
    router  = express.Router(),
    User    = require('../models/user'),
    DailySubscriber    = require('../models/dailySubscriber');

router.get('/', function(req, res, next) {
    var userObject = {
        "query": {isSubscribed: true},
        "out": "dailySubscriber"
    };
    userObject.map = function () { 
        var date = new Date(this.subscribedAt.valueOf() - (this.subscribedAt.valueOf() % (1000 * 60 * 60 * 24 )));
        emit(date, 1) 
    }
    userObject.reduce = function (k, vals) { return vals.length }
    User.mapReduce(userObject, function (err, results) {
    //console.log(results)
    })

    var subscriberObject = {
        "scope": { "total": 0 },
        "finalize": function(key, value) {
            total += value;
            return total;
        },
        "out": "totalSubscriber"
    };

    subscriberObject.map = function () { 
        emit(this._id, this.value) 
    }
    subscriberObject.reduce = function (k, vals) { return vals.length }
    DailySubscriber.mapReduce(subscriberObject, function (err, total) {
        console.log("This is the result" + total)
    })

    res.render('dashboard', {title: "Welcome to analytics dashboard!"});
});
module.exports = router;

, node, This is the result undefined , , MongoDB , , , , _id value .

{
    "results" : [
        {
            "_id" : ISODate("2016-05-28T00:00:00Z"),
            "value" : 2
        },
        {
            "_id" : ISODate("2016-05-29T00:00:00Z"),
            "value" : 8
        },
        {
            "_id" : ISODate("2016-05-30T00:00:00Z"),
            "value" : 11
        },
    ]
}

? Node.js, Express Mongoose, .

! !

+4
1

imho - , , , . ( sql) CTE, .

forEach , . , . :

var previous = 0;
forEach (var r in result)
{
    r.count += previous;
    previous = r.count;
}
0

All Articles