-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Environment:Windows 7 + mongodb 1.6.2
and
Linux + mongodb 1.6.2
-
ALL
I used the following collection and funtions (based on "MapReduce
Example 2" http://www.mongodb.org/display/DOCS/MapReduce and
"Multikeys (Indexing Values in an Array)"
http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo#FullTex...).
-----------------------------------
db.things.drop();
db.things.ensureIndex(
{name:1, tags:1});
db.things.insert(
{ _id : 1, name : 'name1', tags : ['dog', 'cat'] } );
db.things.insert(
);
db.things.insert(
);
db.things.insert(
);
m = function(){
this.tags.forEach(
function(z){ emit( z ,
); } );
};
r = function( key , values ){
var total = 0;
for ( var i=0; i<values.length; i++ )
total += values[i].count;
return
;
};
res = db.runCommand(
{ mapreduce : 'things', map : m, reduce : r });
db[res.result].find();
-----------------------------------
Expected result:
dog : 3
cat: 2
mouse : 1
Function result:
{ "_id" : "cat", "value" :
}
{ "_id" : "dog", "value" :
}
{ "_id" : "mouse", "value" :
}
OK!
Now the same with a query: count only tags in doctumenmts with
name='name1':
-----------------------------------
res = db.runCommand({ mapreduce : 'things', map : m, reduce : r, query:
{name : 'name1'}});
db[res.result].find();
-----------------------------------
Expected result:
dog : 1
cat: 1
Function result:
{ "_id" : "cat", "value" :
}
{ "_id" : "dog", "value" :
}
Why that????
"count" always contains the total number of elements in the array
'tags'. So with "name : 'name3'" the result is:
{ "_id" : "cat", "value" :
}
{ "_id" : "dog", "value" :
}
{ "_id" : "mouse", "value" :
}
The reduce-function is called once for EACH element of the tags-array
for the SAME tag (tag=key).
If I don't create the index "db.things.ensureIndex(
{name:1, tags:1});"
it works correct.
Ulrich
http://groups.google.com/group/mongodb-user/browse_thread/thread/1f24161d3d1daf01