0

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!

3
  • Your map is wrong for starters try: emit(this.product_id, {"_id" : this._id, "name" : this.name }); Commented Aug 13, 2012 at 20:25
  • Skip the reduce and just in the finalise remove all but [0] of the values rows from the outputted document and bam you have your unique document. Commented Aug 13, 2012 at 20:26
  • 1
    it would be helpful if you explained exactly what the structure of your documents is and what you expect to get as the final result from your map reduce. Commented Aug 14, 2012 at 1:56

1 Answer 1

2

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,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.