Scala and Casbah - error: Option[com.mongodb.DBObject] does not take parameters -
i'm trying fetch doc , it's attribute. when utilize findone method, expect mongodbobject, receive option[com.mongodb.dbobject]. how attribute it? possible mongodbobject instead of this?
scala> var col = mongoclient()("test")("counters") col: com.mongodb.casbah.mongocollection = mongocollection({ "_id" : "some" , "value" : 0}) scala> var doc = col.findone() doc: option[com.mongodb.dbobject] = some({ "_id" : "some" , "value" : 0}) scala> doc("_id") <console>:13: error: option[com.mongodb.dbobject] not take parameters doc("_id") ^ scala>
casbah api doesn't know contents of database , cannot sure record asking exists. in java such method homecoming object can null
. discouraged in scala in faviour of safer option[t]
type. way forced handle situation object didn't exist. have several syntaxes (from worst best:
col.findone().get
when want handle both cases: col.findone() match { case some(r) => //r record case none => //record didn't exist }
when want perform operations on record (monadic style) col.findone().map(r => r("_id")).foreach(println)
the code above print _id
column if such record found - , nil otherwise.
scala casbah
No comments:
Post a Comment