Tuesday 23 May 2017

MongoDB Views Sum

I have MongoDB object called Article:

var articleSchema = mongoose.Schema({
    title:{
        type:String,
        index:true
    },
    views:{
        type:Number,
    },
    active:{
        type: Boolean
    }

});

Now I am trying to get a sum of all articles views. I tried with this module sample, but it's not working. What em am I doing wrong?

module.exports.visitsCounter = function() {

    Article.aggregate([
        {"$group" : {_id:"$views", count:{$sum:1}}}
    ], function(err, result){
        if(err){
            console.log(err);
        }
        console.log("View Incremented!"+result.count);
    });
}



via zire

No comments:

Post a Comment