Thursday 18 May 2017

how to fetch data from outside unwind in mongo?

db.collection.aggregate([{
                $match: {
                    'consumer_id': mongoskin.helper.toObjectID(req.param._id)
                }
            }, {
                $unwind: '$votes'
            }, {
                $group: {
                    _id: "$votes.date",
                    votes: {
                        $sum: {
                            $cond: [{
                                '$gt': ['$votes.score', 0]
                            }, "$votes.score", 0]
                        }
                    }
                }
            }]

my mondodb structure is like this:

{
        "_id" : ObjectId("5902f2ab876525401656e50b"),
        "consumerid" : ObjectId("579ef8466a0aa680093ee22f"),
        "user_id" : "5902f17363ada5b816bd32e3",
        "votes" : [
                {
                        "date" : "2017/3/28",
                        "score" : 1
                }
        ]
}
{
        "_id" : ObjectId("5902f2c3876525401656e50c"),
        "consumerid" : ObjectId("579f030a9b49a0840409df84"),
        "user_id" : "5902f17363ada5b816bd32e3",
        "votes" : [
                {
                        "date" : "2017/3/28",
                        "score" : 1
                }
        ]
}

i would like to fetch the user_id from db using aggregate function along with _id and votes inside the group, so is there any way to fetch the data which is not present in the unwind limit or is it possible to do any alteration here please thank you in advance



via Schüler

No comments:

Post a Comment