[SERVER-1787] incorrect results: map/reduce + query + index on an array Created: 13/Sep/10  Updated: 12/Jul/16  Resolved: 16/Jan/11

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 1.7.5

Type: Bug Priority: Major - P3
Reporter: Ulrich Ahrendt Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 + mongodb 1.6.2
and
Linux + mongodb 1.6.2


Operating System: ALL
Participants:

 Description   

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(

{ _id : 2, name : 'name2', tags : ['cat'] }

);
db.things.insert(

{ _id : 3, name : 'name3', tags : ['mouse', 'cat', 'dog'] }

);
db.things.insert(

{ _id : 4, name : 'name4', tags : [] }

);

m = function(){
this.tags.forEach(
function(z){ emit( z ,

{ count : 1 }

); } );
};

r = function( key , values ){
var total = 0;
for ( var i=0; i<values.length; i++ )
total += values[i].count;
return

{ count : total }

;
};

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" :

{ "count" : 3 }

}
{ "_id" : "dog", "value" :

{ "count" : 2 }

}
{ "_id" : "mouse", "value" :

{ "count" : 1 }

}

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" :

{ "count" : 2 }

}
{ "_id" : "dog", "value" :

{ "count" : 2 }

}

Why that????

"count" always contains the total number of elements in the array
'tags'. So with "name : 'name3'" the result is:
{ "_id" : "cat", "value" :

{ "count" : 3 }

}
{ "_id" : "dog", "value" :

{ "count" : 3 }

}
{ "_id" : "mouse", "value" :

{ "count" : 3 }

}

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



 Comments   
Comment by auto [ 16/Jan/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

Message: some mr tests for SERVER-1787 SERVER-2272
https://github.com/mongodb/mongo/commit/ba78158d2f93394032daa2890e29777b29bf3192

Comment by Eliot Horowitz (Inactive) [ 16/Jan/11 ]

was actually fixed earler, but not sure exactly when

Generated at Thu Feb 08 02:58:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.