Get the App
SLTechnology News&Howtos  ›  Database  › 

Mgo self-increment id of golang

Shulou Source: shulou.com Published: 2022-06-01 15:02:55 11月16日 Update

Import (

"fmt"

"gopkg.in/mgo.v2"

"gopkg.in/mgo.v2/bson"

".. /.. / funcs"

)

/ / id increments from 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

}

Tags: Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Huawei Shulou Information Linux