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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog