网站建设资讯

NEWS

网站建设资讯

golang之mgo自增id

import (
  "fmt"
  "gopkg.in/mgo.v2"
  "gopkg.in/mgo.v2/bson"
  "../../funcs"
)

// id从1开始递增
func GenId(db *mgo.Database) (int64, error) {
  IDInt64 := struct {
     Value int64 `bson:"max_id"`
  }{Value: 1}
  _, err := db.C("gen_id").Find(bson.M{}).Apply(mgo.Change{Update: bson.M{"$inc": IDInt64}, Upsert: true, ReturnNew: true}, &IDInt64)
  return IDInt64.Value, err
}


新闻名称:golang之mgo自增id
文章链接:http://cdweb.net/article/gseoec.html