The way connection is implemented is quite lazy.
val col = MongoConnection("localhost")("myDB")("myCol")
just creates a description of a connection, does not actually try to connect to DB unless you perform some operation on it.
The same goes for col.find()
- it returns a cursor that is not evaluated yet. In other words it's not an open cursor in DB, but a lazy Scala cursor. Once you try to evaluate it by iterating over it it will try to actually run it and will fail.
This is current behavior for Casbah that uses deprecated Mongo
Java class: def apply(host: String): MongoConnection = new MongoConnection(new com.mongodb.Mongo(host))
in MongoConnection
object. It's suggested to use MongoClient
instead of MongoConnection
but it will behave in the same way in terms of when it connects to DB.