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 相信营销号手欠改了处理器设置
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)