Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created September 10, 2014 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxdot/7e42a24b018d70e0b1cb to your computer and use it in GitHub Desktop.
Save boxdot/7e42a24b018d70e0b1cb to your computer and use it in GitHub Desktop.
MapReduce of MongoDB puts the result in `values` field. Sometimes one wants the result to be a document and not a pair `(_id, values)`. The function below flattens the result of map_reduce.
function(table) {
db[table].find().forEach(function(x) {
var update = { $set: {}, $unset: { value: '' } }
for (var k in x.value)
update.$set[k] = x.value[k]
db[table].update({_id: x._id}, update)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment