I'm trying to use mongodb client from scala. I use IntelliJ IDEA.
In this line:
val settings: MongoClientSettings = MongoClientSettings
.builder()
.clusterSettings(clusterSettings)
.build()
I get error:
clusterSettings is not a member of com.mongodb.MongoClientSettings.Builder
Though dependencies and imports seem to be correct.
Here is sbt file:
scalaVersion := "2.11.12"
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "2.3.0"
libraryDependencies += "org.mongodb" % "mongodb-driver-core" % "3.7.0"
And here are includes:
import com.mongodb.ServerAddress
import org.mongodb.scala.MongoClientSettings
import org.mongodb.scala.connection.ClusterSettings
It seems that nobody has this problem out in the internet, but my colleague had the same issue recently. Any ideas what is the problem here?
UPDATE:
As per comment from Ross i've modified sbt:
scalaVersion := "2.11.12"
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "2.3.0"
And modified code:
import com.mongodb.connection.ClusterSettings
import org.mongodb.scala.MongoClientSettings
val settings: MongoClientSettings = MongoClientSettings
.builder()
.applyToClusterSettings((b: ClusterSettings.Builder) => b.applySettings(clusterSettings))
.build()
Now IDEA does not complain and it sees all API, but during compilation i get error:
Error:(9, 60) type mismatch; found : com.mongodb.connection.ClusterSettings.Builder => com.mongodb.connection.ClusterSettings.Builder required: com.mongodb.Block[com.mongodb.connection.ClusterSettings.Builder] .applyToClusterSettings((b: ClusterSettings.Builder) => b.applySettings(clusterSettings))
But if I define block outside and then use it - it compiles successfully:
val block: Block[ClusterSettings.Builder] = new Block[ClusterSettings.Builder] {
override def apply(t: ClusterSettings.Builder): Unit = {
t.applySettings(clusterSettings)
}
}
val settings: MongoClientSettings = MongoClientSettings
.builder()
.applyToClusterSettings(block)
.build()
What could be the reason?
There was a change to the type alias in 2.3.0 for MongoClientSettings
and that has a slightly different API to the previous builder. See the upgrade guide: http://mongodb.github.io/mongo-scala-driver/2.3/upgrade/#mongoclientsettings
The reason it was changed is because the com.mongodb.async.client.MongoClientSettings
was deprecated in favour of a new centralised MongoClientSettings
.
You can still use the deprecated settings for now. Update (refresh) your sbt project and intellij will highlight the lack of a clusterSettings
method on the builder.
An example of applying settings to the new MongoClientSettings
:
import com.mongodb.connection.ClusterSettings
// Scala 2.12 (support for Single Abstract Methods)
val clientSettings: MongoClientSettings = MongoClientSettings
.builder()
.applyToClusterSettings((b: ClusterSettings.Builder) => b.applySettings(clusterSettings))
.build()
// Scala 2.11 you must provide the block:
val clientSettings: MongoClientSettings = MongoClientSettings
.builder()
.applyToClusterSettings(new Block[ClusterSettings.Builder]() {
override def apply(b: ClusterSettings.Builder): Unit = b.applySettings(clusterSettings)
})
.build()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With