Hi,
Can I do update inside the map function for the same document? for example I have something like below :
{
"_id" : 1,
"name" : "Bob",
"species" : "Penguin",
"eat" : 4,
"status" : 0
},
{
"_id" : 2,
"name" : "Ray",
"species" : "Tiger",
"eat" : 10,
"status" : 1
}
So I do filtering at the beginning before I do the map/reduce with below filtering :
BasicDBObject filter = new BasicDBObject();
filter.put("status", new BasicDBObject("$ne", 0));
MapReduceOutput out = animal_collection.mapReduce(map, reduce, out, MapReduceCommand.OutputType.MERGE, filter);
And my map function looks like below :
function(){
var _id = this._id;
var eat = this.eat;
var species = this.species;
db.animal_collection.update({_id : _id}, { $set : { status : 0}});
emit(species,
{
name : name,
eat : eat,
species : species
}
}
reduce :
function(key, values){
var total = 0;
for(var i=0; i<values.length; i++)
{
total += values[i].eat;
}
var result = values[0];
result.eat = total;
return result;
}
Can I do updating like above, since it will directly update into only single document and on the same shard?
Thanks.