Created
June 26, 2015 15:49
-
-
Save bitxel/dfd9815f453826bff60b to your computer and use it in GitHub Desktop.
use mongodb mapreduce to update document example
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 m = function(){ | |
emit(this._id,this) | |
} | |
var r = function(k, value) { | |
// here is the process of update data | |
t = value.running_param.targets; | |
for (var i = 0; i < t.length; i++) { | |
var tmp = t[i].split("/")[1]; | |
if (tmp.indexOf(".") == -1) { | |
if (tmp[0] == 6) { | |
t[i] += ".SH" | |
} else { | |
t[i] += ".SZ" | |
} | |
} | |
} | |
value.running_param.targets = t; | |
// note: return value must be identical to the type of emit value | |
return value; | |
} | |
// reduce func is defined as function(k,v){}, because it won't be executed, | |
// data updating func is defined in finalizer | |
db.collection.mapReduce(m, function(k,v){}, {finalizer:r, out:"out"}) | |
// after mapReduce func, the result is stored in "out" collection with the format of {"_id":ObjectId("ssss","value":{"ss":"bb"}} | |
// then we can use the following command to update it to original | |
db.out.find().forEach(function(item){db.out.update({_id:item._id}, item.value)}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment