mgo 聚类统计

// 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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值