Go使用mgo增删改查聚合操作

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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值