Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1787

incorrect results: map/reduce + query + index on an array

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.7.5
    • 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(

      { _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

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            ua Ulrich Ahrendt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: