dongshan2004 2019-02-11 09:37
浏览 313
已采纳

官方mongo-go-driver使用会话

Is there any example of using sessions with official mongodb driver for golang? I am trying to use sessions to take advantage from transactions and it seems that just reading tests on github I can’t find the way to do it.

To be more specific I am struggling now with this:

session, err := pool.StartSession()                                              
   if err != nil {                                                                  
     log.Println("Could not create db session", err)                                
     return events.APIGatewayProxyResponse{                                         
       Body:       err.Error(),                                                     
       StatusCode: http.StatusInternalServerError,                                  
     }, err                                                                         
   }                                                                                
   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)          
   defer cancel()                                                                   
   defer session.EndSession(ctx)                                                    

    var db *mongo.Database                                                           

Everything fine with code above but when I do:

db = session.Database("testrest")

It gives the error:

session.Database undefined (type mongo.Session has no field or method Database)

Which seems to work with mgo package… How do I select the database and run queries with session?

  • 写回答

1条回答 默认 最新

  • dqwh1205 2019-02-11 22:19
    关注

    The solution for my problem was found here: https://github.com/simagix/mongo-go-examples/blob/master/examples/transaction_test.go#L68

    if session, err = client.StartSession(); err != nil {
            t.Fatal(err)
        }
        if err = session.StartTransaction(); err != nil {
            t.Fatal(err)
        }
        if err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {
            if result, err = collection.UpdateOne(sc, bson.M{"_id": id}, update); err != nil {
                t.Fatal(err)
            }
            if result.MatchedCount != 1 || result.ModifiedCount != 1 {
                t.Fatal("replace failed, expected 1 but got", result.MatchedCount)
            }
    
            if err = session.AbortTransaction(sc); err != nil {
                t.Fatal(err)
            }
            return nil
        }); err != nil {
            t.Fatal(err)
        }
        session.EndSession(ctx)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵