Last active
May 10, 2019 10:23
-
-
Save kikofernandez/5548409 to your computer and use it in GitHub Desktop.
Example of a MongoDB map-reduce with a composite key, sorting the result set in the reduce operation. JavaScript example and its correspondent equivalent in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.books.mapReduce(mapFn, reduceFn, { out: { reduce: 'books_rating'}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
_id: { | |
rating: 4, | |
author: X | |
}, | |
value: [ | |
{ | |
title: 'Le petit prince', | |
author: 'Antoine de Saint-Exupéry', | |
ISBN: 9780440842149, | |
description: 'Description' | |
}, | |
... | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mapFn = function(){ | |
var value = { | |
title: this.title, | |
author: this.author, | |
ISBN: this.ISBN, | |
description: this.description | |
}; | |
emit({rating: this.rating, author: this.author.id}, value); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mongo mongo = new Mongo(hosts); | |
String mapFn = "function(){ var value = {"+ | |
"title: this.title,"+ | |
"author: this.author,"+ | |
"ISBN: this.ISBN,"+ | |
"description: this.description"+ | |
"};"+ | |
"emit({rating: this.rating, author: this.author.id}, value);"+ | |
"}"; | |
String reduceFn = "function(key, values){"+ | |
"return values.sort(function(a,b){"+ | |
"return a.title - b.title;"+ | |
"});"+ | |
"};"; | |
com.mongodb.MapReduceCommand cmd = new com.mongodb.MapReduceCommand(mongo.getDB("bookdb").getCollection("books"), | |
mapFn, reduceFn, "books_rating", com.mongodb.MapReduceCommand.OutputType.REDUCE, null); | |
mongo.getDB("bookdb").getCollection("books").mapReduce(cmd); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mapFn = function(){ | |
var value = { | |
title: this.title, | |
author: this.author, | |
ISBN: this.ISBN, | |
description: this.description | |
}; | |
emit({rating: this.rating, author: this.author.id}, value); | |
}; | |
var reduceFn = function(key, values){ | |
return values.sort(function(a,b){ | |
return a.title - b.title; | |
}); | |
}; | |
db.books.mapReduce(mapFn, reduceFn, { out: { reduce: 'books_rating'}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var reduceFn = function(key, values){ | |
return values.sort(function(a,b){ | |
return a.title - b.title; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment