Commits

Dariusz Luksza  committed a274f33

Fix MapReduce on Mongo 1.8+

  • Participants
  • Parent commits f10bcdf

Comments (0)

Files changed (2)

File fan/Collection.fan

   
   **
   ** Perform a map reduce operation - 'map' and 'reduce' are JavaScript functions.
-  ** 'outputCollection' (optional) specifies the collection to store the results in, 
-  ** otherwise uses a temp collection.  'query' specifies a query to narrow the range 
-  ** of objects this is applied to - null will use all objects.  
+  ** 'out' (optional) specifies the collection to store the results in. 'query'
+  ** (optional) specifies a query to narrow the range of objects this is applied
+  ** to - null will use all objects.  
   **
   ** Returns a Map in which the key "result" is the Collection name created for the reults.
   ** Other info - processing time, etc is also included
   **
   ** See http://www.mongodb.org/display/DOCS/MapReduce
   **
-  Str:Obj? mapReduce(Str map, Str reduce, Str:Obj opts := [:])
+  Str:Obj? mapReduce(Str map, Str reduce, Str out := "reduceOut", Str:Obj opts := [:])
   {
     mrcmd := [:] { ordered = true }
-    mrcmd.set("mapreduce", name).set("map", map).set("reduce", reduce)
+    mrcmd.set("mapreduce", name).set("map", map).set("reduce", reduce).set("out", out)
     mrcmd.addAll(opts) // should probably make sure this is merged so above is not overwritten...
     res := db.command(mrcmd)
     if(!DB.cmdOk(res)) throw MongoOpErr("mapReduce failed - ${res}")

File test/CollectionTest.fan

 
     map = "function() { emit(this.user_id, 1); }"
     red = "function(k,vals) { return 1; }"
-    res = c.mapReduce(map, red, ["query": ["user_id": ["\$gt": 1]]])
+    res = c.mapReduce(map, red, "reduceOut", ["query": ["user_id": ["\$gt": 1]]])
     mrcoll = db[res["result"]]
     verifyEq(2, mrcoll.find.count)
     verifyNull(mrcoll.findOne(["_id": 1]))