Execution error when mapping from a MongoDB embedded document

270 Views Asked by At

Suppose I am trying to output an embedded field from a mongoDB (using the Play Framework 2.6 - in JSON). A typical document looks like this:

   _id : ObjectId("66bc9c788c788cafdb053a23"),
   name : "Name game"
   myFieldArr : [
     {
        fieldName : "A playing field"
        fieldGroup: "Landscape"
        numOfGroup: 22
     },
     ...
   ]

I am using $unwind from the Aggregation Framework in reactiveMongo so I have this:

  def getAggregate(col: JSONCollection) = {

    import col.BatchCommands.AggregationFramework.{UnwindField}

    col.aggregate(UnwindField("myFieldArr")).map(_.head[MyAggregate])

  }

I have a case class which I am automatically mapping to the mongoDB documents with Play:

   case class MyField(fieldName: String, fieldGroup: String, numOfGroup: Int) {

   case class MyAggregate(_id: Option[BSONObjectID], name: String, myField: MyField) {
     def idAsBsonId = _id.get
     def idAsString = idAsBsonId.stringify
   }

But I keep getting this error:

[JsResultException: JsResultException(errors:List((/myField,List(JsonValidationError(List(error.path.missing),WrappedArray())))))]

I've been through a bunch of similar questions but none for the purpose of mapping from a mongoDB document so I can't quite get this right. Any pointers? Thanks!

0

There are 0 best solutions below