MongoDB node-mongodb-native map / reduce [英] MongoDB node-mongodb-native map/reduce

查看:126
本文介绍了MongoDB node-mongodb-native map / reduce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个map / reduce方法使用node-mongodb-native。我试图只返回'product_id'不同的记录。这是我到目前为止:

  var map = function(){
emit(_ id,{ _id:this._id,
product_id:this.product_id,
name:this.name
});
}

var reduce = function(key,values){

var items = [];
var exists;

values.forEach(function(value){

if(items.length> 0){

items.forEach ,key){
if(item_val.product_id == value.product_id){
exists = true;
}
});

if !exists){
items.push(value);
}

exists = false;

} else {

items.push(value);
}
});

return {items:items};
}

this.collection.mapReduce(map,reduce,{out:{inline:1},limit:4},function(err,results){
if err){
console.log(err);
} else {
console.log(results [0] .value.items);
}
} ;

我的逻辑似乎无法工作。



任何帮助都会很棒 - 谢谢!

解决方案

实际上,您尝试执行的示例:

  var map = function (){
emit(this.product_id,{_id:this._id,name:this.name});
}

var finailise = function(key,value){
返回值[0]; //这可能需要调整,在一段时间内没有完成MRs:/
}


  • 最后找到 li>


    没有标准的方式来区分,每个DB都有自己的方法,它甚至不是SQL数据库的标准,所以它是由你知道你想要区分哪种方式。上面的一个第一个发现不同。你可以做一个最后一个找到不同的:

      var finailise = function(key,value){
    return value [ value.length-1];
    }

    或类似的东西,应该让你开始。



    希望它有帮助,


    I have a map/reduce method using node-mongodb-native. I'm trying to only return distinct records on 'product_id'. This is what I have so far:

            var map = function() {
                emit("_id", {"_id" : this._id, 
                             "product_id" : this.product_id,
                             "name" : this.name 
                             });
            }
    
            var reduce = function(key, values) {
    
                var items  = [];
                var exists;
    
                values.forEach(function(value) {
    
                    if(items.length > 0) {
    
                        items.forEach(function(item_val, key) {
                            if(item_val.product_id == value.product_id) {
                                exists = true;
                            }
                        });
    
                        if(!exists) {
                            items.push(value);
                        }
    
                        exists = false;
    
                    } else {
    
                        items.push(value);
                    }
                }); 
    
                return {"items" : items};
            }
    
            this.collection.mapReduce(map, reduce, {out : {inline: 1}, limit: 4}, function(err, results) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(results[0].value.items);
                }
            });
    

    Something with my logic doesn't seem to work. It still adds duplicate records where product_id's are the same.

    Any help would be awesome - thank you!

    解决方案

    In fact, an example of what your trying to do:

    var map = function() {
        emit(this.product_id, {"_id" : this._id, "name" : this.name });
    }
    
    var finailise = function(key, value){
        return value[0]; // This might need tweaking, ain't done MRs in a while :/
    }
    

    Do note however there are two types of distinct:

    • First find
    • Last find

    There is no standard way to distinct and each DB has it's own methods, it isn't even standard across SQL databases so it is upto you to know which way you want to distinct. The one above does a first find distinct. You can do a last find distinct like:

    var finailise = function(key, value){
        return value[value.length-1]; 
    }
    

    Or something similar, should get you started anyway.

    Hope it helps,

    这篇关于MongoDB node-mongodb-native map / reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆