Hi,
I want to convert a org.mongodb.scala.bson.collection.immutable.Document to the following case class:
case class Entry(id: String, values: Seq[SubEntry])
case class SubEntry(code: String, value:)
I am unable to figure out how to convert the sequence to the case class.
Here is my attempt with implicit class:
object Helpers {
implicit class DocumentAs(doc: Document) {
def asSubEntry: Option[SubEntry] = {
for {
x <- doc.get[BsonString]("code") map (_.asString().getValue)
y <- doc.get[BsonInt64]("value") map (_.asInt64().getValue)
} yield SubEntry(x, y)
}
def asEntry: Option[Entry] = {
for {
x <- doc.get[BsonString]("id") map (_.asString().getValue)
y <- doc.get[BsonArray]("values") map (_.asArray ???)
} yield Entry(x, y)
}
}
}
def findEntry(id: String): Future[Option[Entry]] = {
val col: MongoCollection[Document] = mongo.db.getCollection("mycol")
for {
doc <- col.find(equal("id", id)).head()
entry <- doc.asEntry
} yield Some(entry)
}
Any hints on how to convert the BsonArray to a Seq[SubEntry] ?
Thanks