duanfu3597
duanfu3597
2019-07-17 12:44

MapReduce中的Golang GlobalSign mgo查询

已采纳
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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpjjr42626 dpjjr42626 2年前

    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)
    
    点赞 评论 复制链接分享
  • duanmao7553 duanmao7553 2年前

    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)
    
    点赞 评论 复制链接分享

为你推荐