distinct
query := func(c *mgo.Collection) (interface{}, error) {
q := bson.M{}
var items []string
err := c.Find(q).Distinct("w_name", &items)
if err != nil {
return nil, err
}
fmt.Println(items)
return items, nil
}
res, err := m.mongo.Execute(m.table, query)
聚合
query := func(c *mgo.Collection) (interface{}, error) {
var pipeline []bson.M
group := bson.M{
"_id": "$w_name",
"widget_id":bson.M{"$first":"$w_id"},
"widget_name":bson.M{"$first":"$w_name"},
}
pipeline = append(pipeline, bson.M{"$group": group})
pipeline = append(pipeline, bson.M{"$sort": bson.M{"w_id": 1}})
var res []struct {
WId int64 `json:"w_id" bson:"w_id"`
WName string `json:"w_name" bson:"w_name"`
}
err := c.Pipe(pipeline).All(&res)
if err != nil {
return nil, err
}
fmt.Println(res)
return res,nil
}
res, err := m.mongo.Execute(m.table, query)