-1
import "github.com/globalsign/mgo"

job := &mgo.MapReduce{
    Map:    "function() { emit(this.name, 1) }",
    Reduce: "function(key, values) { return Array.sum(values) }",
    Out:    "res",

}

_, err = c.Find(nil).MapReduce(job, nil)

How to add 'query' to the above golang mgo mapreduce ?

Ref:

https://docs.mongodb.com/manual/core/map-reduce/ https://godoc.org/github.com/globalsign/mgo#MapReduce

yodhevauhe
  • 765
  • 3
  • 11
  • 33

2 Answers2

3

Managed to get this done with MongoDb official godriver

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

par := bson.D{
    {"mapreduce", "audit"},
    {"map", " function() { emit( this.name , 1 ); }"},
    {"reduce", "function(key, arr) { return Array.sum(arr); }"},
    {"out", "mr3"},
    {"query", bson.D{{"status", "SUCCESS"}}},
}

sr := db.RunCommand(nil, par)
yodhevauhe
  • 765
  • 3
  • 11
  • 33
1

MapReduce is a function of the Query struct returned by Find. So in order to apply your MapReduce to a query result, simply add a query document to the find function:

c.Find(query).MapReduce(job,result)
Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89