dongwen1909 2018-04-03 05:21 采纳率: 0%
浏览 250
已采纳

如何使用golang在mongodb中对具有相同user_id的收集数据进行$ group?

I'm trying to make the sum of the price of the product which having same user_id using golang. But I don't know how I will get this. I tried $group in the following code

Struct for product

type Product struct {
  Id         int     `json:"id" bson:"_id"`
  Name       string  `json:"name" bson:"name"`
  Sku        string  `json:"sku" bson:"sku"`
  Category   string  `json:"category" bson:"category"`
  Stock      int     `json:"stock" bson:"stock"`
  Price      float64 `json:"price" bson:"price"`
  Sale_price float64 `json:"sale_price" bson:"sale_price"`
  UpdatedOn  int64   `json:"updated_on" bson:"updated_on"`
  UserId     int     `json:"user_id" bson:"user_id"`
}

Struct for the customer

type Customer struct {
  Id               int    `json:"id" bson:"_id"`
  FirstName        string `json:"first_name" bson:"first_name"`
  LastName         string `json:"last_name" bson:"last_name"`
  Email            string `json:"email" bson:"email"`
  PhoneNumber      string `json:"phone_number" bson:"phone_number"`
}

function from where the data will retrieve

func GetProducts(c *gin.Context) {
    mongoSession := config.ConnectDb()                                         //connection to database
    collection := mongoSession.DB(config.Database).C(config.ProductCollection) //session
    pipeline := []bson.M{
        bson.M{"$group": bson.M{"user_id": 1}}, bson.M{"$sum": bson.M{"price": "price"}},
    } //query i tried
    fmt.Println(pipeline)
    pipe := collection.Pipe(pipeline)
    resp := []bson.M{}
    err = pipe.All(&resp)
    if err != nil {
        fmt.Println("Errored: %#v 
", err)
    }
    fmt.Println(resp)
    GetResponseList(c, response)
}

The data viewed in the image-

In the above image the user_id of the product is same. I want to group this data and calculate the price.

I also read the documentation of the Mongodb this one but don't understand how to do in golang can anyone explain it in simple way how to make the query for this. Thanks

  • 写回答

1条回答

  • douxing6434 2018-04-03 07:11
    关注

    When grouping using $group, you have to use the special _id property to tell what you want to group by.

    And when referring to existing properties / fields, you have to use the $ sign in front of their names, e.g. $user_id.

    And besides the _id you may list additional properties whose value may be some aggregate value (like $sum).

    This is how your pipeline may look like:

    pipeline := []bson.M{
        {
            "$group": bson.M{
                "_id":       "$user_id",
                "sum_price": bson.M{"$sum": "$price"},
            },
        },
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站