// GroupCountByIssueType return field count list
func (mgr *issueMgr) GroupCountByIssueType(field string, ids ...string) (fieldCount []FieldCount, err error) {
coll := mgo2.FastCopyCollection(mgr.coll)
defer mgo2.CloseCollection(coll)
q1 := bson.M{
"$match": bson.M{
"fields.issuetype.id": bson.M{"$in": ids},
},
}
q2 := bson.M{
"$group": bson.M{
"_id": bson.M{field: "$" + field},
"count": bson.M{"$sum": 1},
},
}
operations := []bson.M{q1, q2}
pipe := coll.Pipe(operations)
var group []fieldGroup
err = pipe.All(&group)
for _, g := range group {
fieldCount = append(fieldCount, FieldCount{
Field: g.ID[field],
Count: g.Count,
})
}
return
}
// FieldCount field count
type FieldCount struct {
Field string `json:"field"`
Count int `json:"count"`
}
// fieldGroup field group
type fieldGroup struct {
ID map[string]string `bson:"_id,omitempty"`
Count int
}
mgo 聚类统计
最新推荐文章于 2021-09-14 17:51:39 发布