Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 按性别和地区划分的猫鼬mapReduce_Node.js_Mongodb_Mongoose_Mapreduce - Fatal编程技术网

Node.js 按性别和地区划分的猫鼬mapReduce

Node.js 按性别和地区划分的猫鼬mapReduce,node.js,mongodb,mongoose,mapreduce,Node.js,Mongodb,Mongoose,Mapreduce,我正在尝试使用mapReduce方法来获得在特定区域停留的男性和女性人数。我使用下面的代码,然后编写一个逻辑来计算值。我想知道这是否可以在模块内完成。这是可能的,还是我现有的方法就是这样 module.export.findStudentsForGivenAdministrativeAreaInAState = function(administrativeArea){ var o = {}; var self = this; o.map = function () { var valu

我正在尝试使用mapReduce方法来获得在特定区域停留的男性和女性人数。我使用下面的代码,然后编写一个逻辑来计算值。我想知道这是否可以在模块内完成。这是可能的,还是我现有的方法就是这样

module.export.findStudentsForGivenAdministrativeAreaInAState = function(administrativeArea){
var o = {};
var self = this;
o.map = function () {
    var values = {gender:this.gender,area_level_2:this.address.administrative_area_level_2};
    emit(this._id, values)
};
o.reduce = function (k, vals) {
    return vals;
};
o.query = {
    'address.administrative_area_level_1': administrativeArea
};

return new Promise.resolve(Student.mapReduce(o))
    .then(function (data) {
        return data;
    }).catch(function (error) {
        routesLogger.logError("studentDAL", "getDistinctStatesWhereStudentsAreRegistered", error);
        return error;
    });
};

这也可以通过使用来实现,因为聚合在服务器(C++)中本机运行,而mapReduce生成单独的javascript线程来运行javascript代码,因此它具有更好的性能。因此,您可以运行以下管道以获得相同的结果:

var Student = require('../../../models/Student');
module.exports.findStudentsForGivenAdministrativeAreaInAState = function(administrativeArea, callback){
    var pipeline = [
        {
            "$match": { "address.administrative_area_level_1": administrativeArea }
        },
        {
            "$group": {
                "_id": "$address.administrative_area_level_2",
                "men": {
                    "$sum": {
                        "$cond": [ { "$eq": [ "$gender", "male" ] }, 1, 0 ]
                    }
                },
                "women": {
                    "$sum": {
                        "$cond": [ { "$eq": [ "$gender", "female" ] }, 1, 0 ]
                    }
                },
                "count": { "$sum": 1 }
            }
        }
    ];

    Student.aggregate(pipeline, function (err, results) {
        if(err) { callback(err); }
        else {
            console.log(results);
            callback(null, results);
        };
    });
};

这也可以通过使用来实现,因为聚合在服务器(C++)中本机运行,而mapReduce生成单独的javascript线程来运行javascript代码,因此它具有更好的性能。因此,您可以运行以下管道以获得相同的结果:

var Student = require('../../../models/Student');
module.exports.findStudentsForGivenAdministrativeAreaInAState = function(administrativeArea, callback){
    var pipeline = [
        {
            "$match": { "address.administrative_area_level_1": administrativeArea }
        },
        {
            "$group": {
                "_id": "$address.administrative_area_level_2",
                "men": {
                    "$sum": {
                        "$cond": [ { "$eq": [ "$gender", "male" ] }, 1, 0 ]
                    }
                },
                "women": {
                    "$sum": {
                        "$cond": [ { "$eq": [ "$gender", "female" ] }, 1, 0 ]
                    }
                },
                "count": { "$sum": 1 }
            }
        }
    ];

    Student.aggregate(pipeline, function (err, results) {
        if(err) { callback(err); }
        else {
            console.log(results);
            callback(null, results);
        };
    });
};

你能公布预期结果吗?您是否使用“管理区域\级别\ 2”字段指定了特定区域?我确实将该字段指定为导出的绘图。我经过行政区。如果我说2个不同的行政区2级,我希望得到[{id:xxxx,地区2级:1区,男性:2,女性:2,计数:4},{id:xxxx,地区2级:2区,男性:4,女性:4,计数:8}你能公布预期结果吗?您是否使用“管理区域\级别\ 2”字段指定了特定区域?我确实将该字段指定为导出的绘图。我经过行政区。如果我说两个不同的行政区,我希望得到[{身份证号码:xxxx,身份证号码:xxxx,身份证号码:xxxx,身份证号码:2,身份证号码:4},{身份证号码:xxxx,身份证号码:xxxx,身份证号码:2,身份证号码:2,身份证号码:2,身份证号码:2,身份证号码:4,身份证号码:8}]@Raulganguly不必担心。有什么问题吗?@RahulGanguly不必担心。问题是什么?