Cannot find class with optional fields

9 views
Skip to first unread message

Heiko Seeberger

unread,
Oct 21, 2010, 6:02:08 AM10/21/10
to mongo-scala-driver
Hi,

I have got a case class with optional parameters (Option[String]) and an according shape with optional fields (see below). When I insert an object into the collection it is saved as expected:

> var cursor = db.name.find();                      
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("4cc00de3133d2675eff9e887"), "de" : "DE" }

When I try to query it using mongo-scala-driver, it won't be found:

scala> val nameColl = ismDB getCollection "name"
nameColl: com.mongodb.DBCollection = name

scala> val name = nameColl of I18nNameShape     
name: com.osinka.mongodb.shape.ShapedCollection[ch.petag.ism.model.I18nName] = ShapedCollection[ch.petag.ism.model.I18nNameShape$](name)()

scala> nameColl.count
res1: Long = 0

scala> name << I18nName("DE")                   

scala> nameColl.count
res3: Long = 1

scala> name.size
res4: Int = 1

scala> name.headOption
res5: Option[ch.petag.ism.model.I18nName] = None

Here is the code:

trait I18nNameIn[A] extends ObjectIn[I18nName, A] {

  lazy val de = Field.optional("de", _.de)

  lazy val en = Field.optional("en", _.en)

  lazy val fr = Field.optional("fr", _.fr)

  lazy val it = Field.optional("it", _.it)

  lazy val * = de :: en :: fr :: it :: Nil

  def factory(dbo: DBObject): Option[I18nName] = {
    for {
      de(d) <- Some(dbo)
      en(e) <- Some(dbo)
      fr(f) <- Some(dbo)
      it(i) <- Some(dbo)
    } yield I18nName(Option(d), Option(e), Option(f), Option(i))
  }
}

object I18nNameShape extends MongoObjectShape[I18nName] with I18nNameIn[I18nName]

case class I18nName(
  de: Option[String] = None,
  en: Option[String] = None,
  fr: Option[String] = None,
  it: Option[String] = None) extends MongoObject {

  def name(implicit locale: Locale): String = {
    import Locale._
    locale match {
      case GERMAN => de getOrElse mongoOID.toString
      case ENGLISH => de getOrElse mongoOID.toString
      case FRENCH => de getOrElse mongoOID.toString
      case ITALIAN => de getOrElse mongoOID.toString
      case _ => mongoOID.toString
    }
  }
}

Any ideas?

Thank you,

Heiko

Company: weiglewilczek.com
Blog: heikoseeberger.name
Follow me: twitter.com/hseeberger
OSGi on Scala: scalamodules.org
Lift, the simply functional web framework: liftweb.net
Akka - Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Actors: akkasource.org

Heiko Seeberger

unread,
Oct 21, 2010, 6:22:51 AM10/21/10
to mongo-scala-driver
To clarify my issue:

As there is certainly an object in the collection, why does name.headOption return None instead of Some(...)?

Heiko
--
Heiko Seeberger

Heiko Seeberger

unread,
Oct 21, 2010, 6:50:43 AM10/21/10
to mongo-scala-driver
OK, once again answering my own question ;-)

Replacing the factory:

  def factory(dbo: DBObject): Option[I18nName] = {
    Some(I18nName(de from dbo, en from dbo, fr from dbo, it from dbo))
  }

Instead of:

  def factory(dbo: DBObject): Option[I18nName] = {
    for {
      de(d) <- Some(dbo)
      en(e) <- Some(dbo)
      fr(f) <- Some(dbo)
      it(i) <- Some(dbo)
    } yield I18nName(Option(d), Option(e), Option(f), Option(i))

Heiko
Reply all
Reply to author
Forward
0 new messages