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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)