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
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)
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)