如何使用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个回答



使用 $ group </ code>进行分组时,必须使用特殊的 _id </ code>属性 告诉您要分组的内容。</ p>

在引用现有属性/字段时,必须在其名称前使用 $ </ code>符号,例如 $ user_id </ code>。</ p>

除了 _id </ code>之外,您还可以列出其值可能是一些聚合值的其他属性(例如 $ sum </ code>)。</ p>

这是您的管道的外观:</ p>

 管道:= [] bson.M {

{
“ $ group”:bson.M {
“ _id”:“ $ user_id”,
“ sum_price”:bson.M {“ $ sum”:“ $ price”},
},

},
}
</ code> </ pre>
</ div>

展开原文

原文

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"},
        },
    },
}

dpi96151
dpi96151 好的,谢谢,我只是问,因为我正在阅读mongodb的其他文档,这就是为什么
2 年多之前 回复
dpzlz08480
dpzlz08480 您要如何使用$ match? 当然,您可以在管道中使用$ match进行过滤。
2 年多之前 回复
du6333137
du6333137 谢谢你给我这个答案。 有用 。 但我也可以用$ match做吗? @icza
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问