Aggregation in Golang mgo for Mongodb

前端 未结 2 1510
醉话见心
醉话见心 2021-02-14 04:45

Anybody knows what’s the equivalent of aggregate command we use in mongodb shell for golang mgo/bson?

Something like that:

aggregate([{$match:{my_id:Obje         


        
2条回答
  •  无人共我
    2021-02-14 05:29

    Assuming that c is your Collection:

    pipe := c.Pipe([]bson.M{{"$match": bson.M{"name":"John"}}})
    resp := []bson.M{}
    err := pipe.All(&resp)
    if err != nil {
      //handle error
    }
    fmt.Println(resp) // simple print proving it's working
    

    GoDoc references:

    • Collection.Pipe documentation
    • Pipe and its methods

提交回复
热议问题