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)
如何在上述golang mgo mapreduce中添加“查询”?
参考:
https://docs.mongodb.com/manual/core/map-reduce/ https://godoc.org/github.com/globalsign/mgo#MapReduce
答案 0 :(得分:1)
MapReduce
是Find返回的Query结构的函数。因此,为了将您的MapReduce应用于查询结果,只需将查询文档添加到find函数:
c.Find(query).MapReduce(job,result)
答案 1 :(得分:0)
设法通过MongoDb官方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)