golang mongodb sessionid format conversion

37 views Asked by At

How to convert string variable to bson.raw format,let me know what to do, help, beginner thanks a lot

After linking to mongodb, the Sessionid needs to be converted back to the original format as a string

func main() {
    uri := fmt.Sprintf("mongodb://root:[email protected]:32082,172.22.50.25:32083,172.22.50.25:32084/?connectTimeoutMS=300000&authSource=admin")
    opt := options.Client().ApplyURI(uri).SetSocketTimeout(timeout)
    ctx, cancel := context.WithTimeout(context.Background(), timeout)
    defer cancel()
    client, err := mongo.Connect(ctx, opt)
    if err != nil {
        logs.Error("connect mongo failed, err:%s", err.Error())
        return
    }
    err = client.Ping(ctx, readpref.Primary())
    if err != nil {
        logs.Error("ping mongo failed, err:%s", err.Error())
        return
    }

    wc := writeconcern.New(writeconcern.WMajority())
    rc := readconcern.Snapshot()
    var maxCommitTime = time.Second * 10
    //txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc).SetMaxCommitTime(&maxCommitTime)


    sessionOpts := &options.SessionOptions{}
    sessionOpts.SetDefaultWriteConcern(wc).SetDefaultReadConcern(rc).SetDefaultMaxCommitTime(&maxCommitTime)

    session1, _ := client.StartSession(sessionOpts)
    sessionContext := mongo.NewSessionContext(ctx, session1)
    if err := sessionContext.StartTransaction(); err != nil {
        panic(err)
    }
    // Transaction-based crud
    txn(client, session1, sessionContext, ctx)
    as := session1.ID() // bson.raw 
    fmt.Println(as.String())
    ass := as.String() // How to convert ass string variable to bson.raw
        ???       


go.mod

go 1.16

require (
    github.com/google/uuid v1.2.0
    go.mongodb.org/mongo-driver v1.10.6
)

How to convert ass string variable to bson.raw

0

There are 0 answers