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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 如何批量获取json的url
  • ¥15 对法兰连接元件所承受的表面载荷等效转化为法兰开孔接触面上的等效表面载荷?
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼