MongoDB - Mongoose : Aggregate generation, $match $group $project

时间:2015-11-12 10:49:49

标签: node.js mongodb mongoose aggregation-framework

I'm newbie in the mongoDB world. I'm developping a website on nodeJS with expressJS and mongoose as DB.

I'm studying graphic orientation and I am generated a DB with this modele :

var userSchema = new Schema({
    profile:{
        sexe:           String, // ('male' or 'female')
        age:            String, // ('kid', 'adult', 'old')
        rank:           String, // ('low', 'middle', 'high')
    },
    design:{
        luminosity:     String, // ('light', 'dark')
        color:          String, // ('blue', 'green', 'pink', 'orange')
        shape:          String, // ('rounded', 'squared')
        font:           String, // ('serif', 'sans serif')
    }
});

On different page, I want to display some statistics, for exemple, on the page luminosity, where user chooses between light or dark. I want to display him what people mostly pick.

For example, with the section sexe : what mostly pick female (68% of them choose light, for example). I think the best way to get all the numbers to create the statistics is to use aggregate.

As newbie, it destroys my mind to generate the perfect query, I am really lost in this framework !

Would love your advice !

1 个答案:

答案 0 :(得分:1)

Your aggregation operation will be dependant on the $cond operator in the $group pipeline step to evaluate the counts based on the page luminosity and/or page color. Use the $sum accumulator operator to return the sum on each evaluated group, something like the following:

var pipeline = [    
    { 
        "$group": { 
            "_id": "$profile.sexe",             
            "light_count": {
                "$sum": {
                    "$cond": [ { "$eq": [ "$design.luminosity", "light" ] }, 1, 0 ]
                }
            },
            "dark_count": {
                "$sum": {
                    "$cond": [ { "$eq": [ "$design.luminosity", "dark" ] }, 1, 0 ]
                }
            },
            "blue_count": {
                "$sum": {
                    "$cond": [ { "$eq": [ "$design.color", "blue" ] }, 1, 0 ]
                }
            },
            "green_count": {
                "$sum": {
                    "$cond": [ { "$eq": [ "$design.color", "green" ] }, 1, 0 ]
                }
            },
            "pink_count": {
                "$sum": {
                    "$cond": [ { "$eq": [ "$design.color", "pink" ] }, 1, 0 ]
                }
            },
            "orange_count": {
                "$sum": {
                    "$cond": [ { "$eq": [ "$design.color", "orange" ] }, 1, 0 ]
                }
            }               
        }  
    },
    {
        "$project": {
            "_id": 0, "gender": "$_id",            
            "luminosity": {
                "light": "$light_count",
                "dark": "$dark_count"
            },
            "color": {
                "blue": "$blue_count",
                "green": "$green_count",
                "pink": "$pink_count",
                "orange": "$orange_count"
            }
        }
    }
];

User.aggregate(pipeline)
    .exec(function (err, result){
        // handle error
        if (err) throw Error;
        console.log(result);
    })