0

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!

cchantep
  • 9,118
  • 3
  • 30
  • 41
  • First execute the same in MongoShell (and see that there is no expected `myField`) – cchantep Nov 02 '17 at 08:28
  • @cchantep. Thanks - this seems like a while back and I have since resolved the issue that I was having. I tend to initially test in the Mongo Shell and then write the reactiveMongo scala code. I believe that the issue was that one of the parameters in the case class that I was using to map to the Mongo documents was called `myField` rather than `myFieldArr` - at least as far as I recall. I'm finding that nowadays not many of my questions are answered here - you are one of the few that take the time to do that - so thanks! – jesus g_force Harris Nov 02 '17 at 10:18

0 Answers0