Skip to content

Instantly share code, notes, and snippets.

@amanrx07
Last active December 28, 2015 15:59
Show Gist options
  • Select an option

  • Save amanrx07/7525940 to your computer and use it in GitHub Desktop.

Select an option

Save amanrx07/7525940 to your computer and use it in GitHub Desktop.

Revisions

  1. aman singh revised this gist Nov 18, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions map_reduce_sum.js
    Original file line number Diff line number Diff line change
    @@ -24,10 +24,8 @@ var options = {
    db.orders.mapReduce(map, reduce, options)

    /*
    Output:
    > db.order_totals.find()
    { "_id" : "A123", "value" : 750 }
    { "_id" : "B212", "value" : 200 }
    */
  2. aman singh revised this gist Nov 18, 2013. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion map_reduce_sum.js
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,13 @@ var options = {
    out: "order_totals"
    }

    db.orders.mapReduce(map, reduce, options)
    db.orders.mapReduce(map, reduce, options)

    /*
    Output:
    > db.order_totals.find()
    { "_id" : "A123", "value" : 750 }
    { "_id" : "B212", "value" : 200 }
    */
  3. aman singh renamed this gist Nov 18, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. aman singh created this gist Nov 18, 2013.
    24 changes: 24 additions & 0 deletions map_reduce_sum
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    use tutorial

    db.orders.insert({ cust_id: "A123", amount: 500, status: "A" })
    db.orders.insert({ cust_id: "A123", amount: 250, status: "A" })
    db.orders.insert({ cust_id: "B212", amount: 200, status: "A" })
    db.orders.insert({ cust_id: "A123", amount: 300, status: "D" })


    // Calculate the sum of all “amount”s for a unique “cust_id”

    var map = function() {
    emit(this.cust_id, this.amount);
    }

    var reduce = function(keyCustId, values) {
    return Array.sum(values);
    }

    var options = {
    query: { status: "A" },
    out: "order_totals"
    }

    db.orders.mapReduce(map, reduce, options)