0

In the official Casbah guide on querying it says

findOne Returns an Option - either Some(MongoDBObject) or None

However, findOne actually gives me an Option with a plain Some(DBObject) instead:

scala> MongoClient("localhost", 27017)("db")("collection").findOne()

res0: Option[com.mongodb.DBObject] = Some({ "_id" : { "$oid" : "559860491b07c0dc5b52f3ee"} , "description" : "Some text" })

The same happens with find, it returns an iterator over DBObject instances. After hours of googling and scratching my head I'm at a loss.

What's going on here? I'm still learning Scala so there might be some nuance that I'm not getting.

James
  • 209
  • 1
  • 2
  • 7
  • The examples in the [tutorial](http://api.mongodb.org/scala/casbah/2.0/tutorial.html) return plain `DBObject`s, so possibly this is just an error in the documentation? And I think casbah provides implicit (automatic) conversions between `DBObject` and `MongoDBObject` - see [here](https://osdir.com/ml/mongodb-user/2011-02/msg02745.html) and [here](http://api.mongodb.org/scala/casbah/2.0/tutorial.html#mongodbobject-a-scala-ble-dbobject-implementation) – DNA Jul 05 '15 at 08:19
  • You're right, thank you! What I missed was the import of `com.mongodb.casbah.Imports._`, the implicit conversion worked after that. I kinda wish they would have made that more clear in the guide. – James Jul 05 '15 at 09:08
  • Great! - I'll add that as an answer... – DNA Jul 05 '15 at 09:38

1 Answers1

1

The examples in the tutorial return plain DBObjects, so I think this is just a mistake in the documentation.

Casbah provides implicit (automatic) conversions between DBObject and MongoDBObject - as described here:

There is an implicit conversion loaded that can Pimp any DBObject as MongoDBObject

We automatically provide implicit methods which convert DBObject to MongoDBObject. Any method which needs a MongoDBObject will accept a DBObject fine as long as you ran the imports.

You will need to import the conversions for this to work, using:

import com.mongodb.casbah.Imports._
DNA
  • 42,007
  • 12
  • 107
  • 146