karlseguin/the-little-mongodb-book

Idempotency Examples

Closed this issue · 6 comments

Hi Karl

First, thanks you so much for this excellent book. I'm fairly new to mongo and this book has been a lifesaver on my most recent project.

I'm scratching my head though about the map reduce idempotency example. Unless I'm still not grasping some fundamental aspect of the M/R functionality the two examples given are not equal. Examples A and B (my notation) would result in 3 and 5 respectively no?

A:
{resource: 'index', year: 2010, month: 0, day: 20} => [{count: 1}, {count: 1}, {count:1}]

B:
{resource: 'index', year: 2010, month: 0, day: 20} => [{count: 1}, {count: 1}]
{resource: 'index', year: 2010, month: 0, day: 20} => [{count: 2}, {count: 1}]

If I do understand correctly then a proper example would be similar to this one from the mongodb website

http://docs.mongodb.org/manual/applications/map-reduce/#ensure-reduce-function-idempotentcy

Also, as a tiny side note, several of the examples leave off the closing "}".

Thanks

Herby
.

Hmmm..I'm pretty sure it's right as-is. Look at our reduce. It set sum = 0 then adds all of the counts.

So you have three documents being passed into reduce:
{count: 1},
{count: 1},
{count: 1}

(a little boring, but we'll stick with that)

One way for reduce to be called is with an array of all three values, in which case the reduce will only be called once and the output is {count:3}

function doIt(key, values) {
  var sum = 0;
  values.forEach(function(value) {
    sum += value[’count’];
  });
  return {count: sum};
};
var result = doIt([{count:1},{count:1},{count:1}])
//result == {count:3}

Another way is for it to be called in chunks. Let's say the first call gets two documents:

var intermediate = doIt([{count:1},{count:1}])
//intermediate == {count: 2}

That means that the next call will be:

var final = doIt([intermediate,{count:1}])  //or, expanded, it would be [{count:2}, {count:1}]
//final == {count: 3}

Been a bit since I looked at this, but I'm convincing myself that I'm right as I write it!

I took a shot at improving the example. See #28.

Hi Karl

Thanks so much for the follow up. I hope I didn't create any unnecessary
work for you.

By the way I loved your Redis book as well.

Keep up the great work.

Herby

On Fri, Feb 1, 2013 at 6:39 PM, Adam Monsen notifications@github.comwrote:

I took a shot at improving the example. See #28#28
.


Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-13019912.

Hi Adam thanks for the taking a look at this.

Herby

With some small tweaks to Adam's change, I hope it's a bit clearer now.

Looks good to me! :shipit:

I think this issue can be closed.