MongoDB写操作的ConnectionNotInitialized异常

时间:2016-04-11 20:12:56

标签: mongodb scala reactivemongo database

我有两个Docker容器:

  1. 运行MongoDB
  2. 使用ReactiveMongo连接到所述MongoDB的服务
  3. 我用来连接MongoDB的代码:

    array_search($post_title, 
         array_map(function($object) {
                 return $object->post_title;
             },
             $yourArray
         )
    );
    

    连接似乎很好,因为我可以毫无问题地阅读它。但是一旦我尝试插入或更新文档,我就会遇到以下异常:

    trait MongoHelper {
      this: BaseConfig =>
    
      val dbKey: String
      val collKey: String
      lazy val mongoConfig = persistenceConfig.getConfig( dbKey )
      lazy val servers = persistenceConfig.getStringList( s"$dbKey.servers" ).asScala
      lazy val database = persistenceConfig.getString( s"$dbKey.database" )
      lazy val username = persistenceConfig.getString( s"$dbKey.username" )
      lazy val password = persistenceConfig.getString( s"$dbKey.password" )
      lazy val credentials = Seq( Authenticate( database, username, password ) )
    
    
      lazy val conOpts = MongoConnectionOptions( authSource = Some( database ), authMode = ScramSha1Authentication )
    
      lazy val driver = new MongoDriver
      lazy val connection = driver.connection( servers, authentications = credentials, options = conOpts )
    
      lazy val db = connection( database )
    
    }
    

    但这对我没有意义,必须初始化连接池,因为我正在使用它来从数据库中读取。

0 个答案:

没有答案