从MongoDB嵌入式文档映射时执行错误

时间:2017-10-09 20:06:23

标签: mongodb reactivemongo reactivemongo-play-json

假设我正在尝试从 mongoDB 输出嵌入字段(使用 Play Framework 2.6 - 在JSON中)。典型文档如下所示:

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

我正在使用$unwind from the Aggregation Framework in reactiveMongo所以我有这个:

  def getAggregate(col: JSONCollection) = {

    import col.BatchCommands.AggregationFramework.{UnwindField}

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

  }

我有一个案例类,我是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
   }

但我一直收到这个错误:

  

[JsResultException:   JsResultException(错误:列表((/ MyField的,列表(JsonValidationError(列表(error.path.missing),WrappedArray())))))]

我已经浏览了一堆similar questions但没有一个用于从mongoDB文档进行映射,所以我无法做到这一点。有什么指针吗?谢谢!

0 个答案:

没有答案