Monday, 22 May 2017

Group data by dates and then dates

1)Group by date, then total number of upvotes and total number of downvotes 2) Group by Category then aggregation has to be done by total number of upvote of all category and downvote of all category by each day upvotes and down votes of individual dates. Here is my code please check and do needfull needful:

db.collection.aggregate([{
                                $unwind: '$votes'
                            }, {
                                $match: {
                                    'category_id': array[i]
                                }
                            }, {
                                $group: {
                                    _id:'$votes.date',
                                    "category_id": {
                                        $first: "$category_id"
                                    },
                                    up_vote: {
                                        $sum: {
                                            $cond: [{
                                                '$gt': ['$votes.score', 0]
                                            }, "$votes.score", 0]
                                        }
                                    },
                                    down_vote: {
                                        $sum: {
                                            $cond: [{
                                                '$lt': ['$votes.score', 0]
                                            }, "$votes.score", 0]
                                        }
                                    }
                                }
                            }, {
                                "$group": {
                                    "_id": "$_id",
                                    "categories": {
                                        "$push": {
                                            "category_id": "$category_id",
                                            "up_vote ": "$up_vote",
                                            "down_vote": "$down_vote"
                                        }
                                    },
                                    "total_up_vote": {
                                        $sum: {
                                            $cond: [{
                                                '$lt': ['$votes.score', 0]
                                            }, "$votes.score", 0]
                                        }
                                    },
                                    "total_down_vote": {
                                        "$sum": "$down_vote"
                                    }
                                }
                            }{
                                "$unwind": "$categories"
                            },
                            {
                                "$project": {
                                    "category_id": "$categories.category_id",
                                    "down_vote": "$categories.down_vote",
                                    "down_vote_Percentage": {
                                        "$multiply": [{ "$divide": [ "$categories.down_vote", "$total_down_vote" ] },
                                            100
                                        ]
                                    },
                                     "up_vote": "$categories.up_vote",
                                    "up_vote_Percentage": {
                                        "$multiply": [{ "$divide": [ "$categories.down_vote", "$total_total_up_vote" ] },
                                            100
                                        ]
                                    }
                                }

                           }
                            ], function(err, results) {
        res.send(result)
        })

This is my database structure:

 "_id" : ObjectId("590f1ab8a45e6eb418be32cd"),
        "category_id" : "singer",
        "celebrity_id" : ObjectId("591e71884e743d8015fd1ae0"),
        "user_id" : "591e81277bd0b65c141e64be",
        "votes" : [
                {
                        "date" : "2017/4/7",
                        "score" : -1
                },
                {
                        "date" : "2017/4/19",
                        "score" : -1
                }
        ]
}
{
        "_id" : ObjectId("59204135dab356f410d1b8a6"),
        "category_id" : "actor",
        "celebrity_id" : ObjectId("591e80e47bd0b65c141e64bc"),
        "user_id" : "591974b64abd73701dc7c4aa",
        "votes" : [
                {
                        "date" : "2017/4/20",
                        "score" : 1
                }
        ]
}
{
        "_id" : ObjectId("5920415cdab356f410d1b8a7"),
        "category_id" : "actor",
        "celebrity_id" : ObjectId("591e81177bd0b65c141e64bd"),
        "user_id" : "591974b64abd73701dc7c4aa",
        "votes" : [
                {
                        "date" : "2017/4/20",
                        "score" : 1
                }
        ]
}



via Rakshitha T.R

No comments:

Post a Comment