Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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 如何填充地图简化结果_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何填充地图简化结果

Node.js 如何填充地图简化结果,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,在地图缩小部分,我获得了客户的积分 现在我想填充_customer以获取客户信息 model.find() 如何执行此操作?在MongoDB中执行mapReduce时,必须确保发射值的格式与reduce返回值的格式相同 这意味着在您的情况下,如果map调用emit(customer,points),那么reduce必须只返回points的值 要更改您的特定示例,可以使用map函数,但reduce函数应为: TestSessionSchema.statics.getTopTesters = fun

在地图缩小部分,我获得了客户的积分

现在我想填充_customer以获取客户信息
model.find()


如何执行此操作?

在MongoDB中执行mapReduce时,必须确保发射值的格式与reduce返回值的格式相同

这意味着在您的情况下,如果
map
调用
emit(customer,points)
,那么
reduce
必须只返回
points
的值

要更改您的特定示例,可以使用map函数,但reduce函数应为:

TestSessionSchema.statics.getTopTesters = function(callback){
    var o = {};
    o.map = function(){
        emit(this._customer, this.points);
    };

    o.reduce = function(_customer, values){
        var result = {_customer: _customer, sum_points: 0, };
        values.forEach(function(point){
            result.sum_points += point;
        });
        return result;
    };

    o.out = { replace: 'createdCollectionNameForResults' };
    o.verbose = true;

    this.mapReduce(o,function(err, model, stats){
        model.find().sort({'value.sum_points': "desc"}).limit(5).populate("_customer").exec(function(error, results){

            log(results);

        })
    });

}
然后,reduce函数将汇总每个客户的所有值(点),这就是您想要的。输出将是_customer,点作为键值对,字段名为
\u id
value
,其中value是该_id(_customer)的所有点值之和

要查询结果,请运行:


db.createdCollectionNameForResults.find().sort({value:-1}).limit(5)

在MongoDB中执行mapReduce时,必须确保emit值的格式与reduce返回值的格式相同

这意味着在您的情况下,如果
map
调用
emit(customer,points)
,那么
reduce
必须只返回
points
的值

要更改您的特定示例,可以使用map函数,但reduce函数应为:

TestSessionSchema.statics.getTopTesters = function(callback){
    var o = {};
    o.map = function(){
        emit(this._customer, this.points);
    };

    o.reduce = function(_customer, values){
        var result = {_customer: _customer, sum_points: 0, };
        values.forEach(function(point){
            result.sum_points += point;
        });
        return result;
    };

    o.out = { replace: 'createdCollectionNameForResults' };
    o.verbose = true;

    this.mapReduce(o,function(err, model, stats){
        model.find().sort({'value.sum_points': "desc"}).limit(5).populate("_customer").exec(function(error, results){

            log(results);

        })
    });

}
然后,reduce函数将汇总每个客户的所有值(点),这就是您想要的。输出将是_customer,点作为键值对,字段名为
\u id
value
,其中value是该_id(_customer)的所有点值之和

要查询结果,请运行:


db.createdCollectionNameForResults.find().sort({value:-1}).limit(5)

您在“结果”中(或在发出中)使用的格式不正确。emit的第二部分必须与reduce返回的“result”格式相同。所以要么发射(this.\u customer,{u customer:this.\u customer,sum.\u points:this.points}),要么将结果格式简化为“result=0”为了表示points.btw,如果您将潜在问题分开,这将非常有用。请从createdCollectionNameForResults集合中进行简单选择,以确保您输出的是您认为自己是的内容。如果看起来没有问题,请尝试第二部分,并在“不起作用”时发布错误您在“result”(或在emit中)中使用的格式不正确。emit的第二部分必须与reduce returns作为“result”的格式相同。因此,可以使用emit(this.\u customer,{\u customer:this.\u customer,sum_points:this.points}或将结果格式简化为“result=0”为了表示points.btw,如果您将潜在问题分开,这将非常有用。请从createdCollectionNameForResults集合中进行简单选择,以确保您输出的是您认为自己是的内容。如果看起来没有问题,请尝试第二部分,并在“不起作用”时发布错误