包
github.com/globalsign/mgo
查询
查询所有
query := func(c *mgo.Collection) (interface{}, error) {
q := bson.M{"widget_id": param.WidgetId, "chart_id": param.ChartId, "is_show": bson.M{"$ne": 0}}
var items []new_panel.Metric
if err := c.Find(q).All(&items); err != nil {
return nil, err
}
return items, nil
}
result, err := m.mongo.Execute(m.table, query)
if err != nil {
return nil, err
} else {
return result, nil
}
查询一个
query := func(c *mgo.Collection) (interface{}, error) {
q := bson.M{"metric_id": item.MetricId, "widget_id": item.WidgetId, "chart_id": item.ChartId, "is_show": bson.M{"$ne": 0}}
if err := c.Find(q).One(&metric); err != nil {
return metric, err
}
return metric, nil
}
result, err := m.mongo.Execute(m.table, query)
temp, _ := json.Marshal(result)
err = json.Unmarshal(temp, &metric)
if metric.MetricId == "" {
return nil, err
}
if err != nil {
return nil, err
}
修改
query = func(c *mgo.Collection) (interface{}, error) {
q := bson.M{"metric_id": metric.MetricId, "widget_id": metric.WidgetId, "chart_id": metric.ChartId, "is_show": bson.M{"$ne": 0}}
if err = c.Update(q, metric); err != nil {
return metric, err
}
return metric, nil
}
if _, err = m.mongo.Execute(m.table, query); err != nil {
return metric, err
}
新建
metric.Id = bson.NewObjectId()
metric.IsShow = 1
metric.MetricName = string(time.Now().Unix())
metric.CreateTime = time.Now().Unix()
metric.SaveTime = time.Now().Unix()
if metric.WidgetId == "" {
metric.WidgetId = uuid.New().String()
}
if metric.MetricId == "" {
metric.MetricId = uuid.New().String()
}
query = func(c *mgo.Collection) (interface{}, error) {
return metric.Id.Hex(), c.Insert(metric)
}
if _, err = m.mongo.Execute(m.table, query); err != nil {
return metric, err
} else {
return metric, nil
}
Upsert
query := func(c *mgo.Collection) (interface{}, error) {
for _, v := range param.Info {
query := bson.M{"chart_id": param.ChartId, "elem_id": v.ElemId}
change := bson.M{
"chart_id": param.ChartId,
"elem_id": v.ElemId,
"elem_name": v.ElemName,
"widget_id": v.WidgetId,
"widget_name": v.WidgetName,
"create_time": time.Now().Unix(),
"save_time": time.Now().Unix(),
}
_, err := c.Upsert(query, change)
if err != nil {
return nil, err
}
}
return nil, nil
}
result, err := m.mongo.Execute(m.table, query)
fmt.Println(result)
if err != nil {
return err
} else {
return nil
}
聚合
query := func(c *mgo.Collection) (interface{}, error) {
var pipeline []bson.M
match := bson.M{
"chart_id": ChartId,
"is_show": 1,
}
group := bson.M{
"_id": bson.M{"chart_id": "$chart_id", "widget_name": "$widget_name"},
"widget_id": bson.M{"$first": "$widget_id"},
"widget_name": bson.M{"$first": "$widget_name"},
}
sort := bson.M{"widget_id": 1}
pipeline = append(pipeline, bson.M{"$match": match})
pipeline = append(pipeline, bson.M{"$group": group})
pipeline = append(pipeline, bson.M{"$sort": sort})
var res []struct {
WidgetId string `json:"widget_id" bson:"widget_id"`
WidgetName string `json:"widget_name" bson:"widget_name"`
}
err := c.Pipe(pipeline).All(&res)
if err != nil {
return nil, err
}
return res, nil
}
res, err := m.mongo.Execute(m.table, query)
if err != nil {
return nil, err
} else {
return res, nil
}