duan1979768678 2014-10-19 23:04
浏览 486
已采纳

Mongodb中Golang mgo中的聚合

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:ObjectId("543d171c5b2c1242fe0019")}},{$sort:{my_id:1, dateInfo:1, name:1}},{$group:{_id:"$my_id", lastEntry:{$max: "$dateInfo"},nm:{$last:"$name"}}}])
  • 写回答

2条回答 默认 最新

  • dongyi8795 2014-10-20 07:44
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失