4

为了在 MongoDB 中进行地理空间查询,具有位置(带有2d2dsphere地理空间索引)的文档应如下所示:

{
    _id: …,
    loc: {
        type: "Point",
        coordinates: [ <longitude>, <latitude> ]
    }
}

我对 Scala、ReactiveMongo 和 Play 框架非常陌生,但在我看来,使用这样一个位置的一个明显方法是通过一个案例类,例如:

case class Point(lon: Double, lat: Double)

网站 API 处理的 JSON 表示应该类似于:

{
    _id: …
    loc: [ <longitude>, <latitude> ]
}

现在,我不知道如何告诉我的 ReactiveMongo 模型在这些格式之间进行序列化/反序列化。

我的控制器如下所示:

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.json._
import scala.concurrent.Future

// Reactive Mongo imports
import reactivemongo.api._
import scala.concurrent.ExecutionContext.Implicits.global

// Reactive Mongo plugin
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection

object Application extends Controller with MongoController {
    def collection: JSONCollection = db.collection[JSONCollection]("test")

    import play.api.data.Form
    import models._
    import models.JsonFormats._

    def createCC = Action.async {
        val user = User("John", "Smith", Point(-0.0015, 51.0015))
        val futureResult = collection.insert(user)
        futureResult.map(_ => Ok("Done!"))
    }
}

我尝试使用 PointWriter 和 PointReader。这是我的models.scala:

package models

import reactivemongo.bson._
import play.modules.reactivemongo.json.BSONFormats._

case class User(
    // _id: Option[BSONObjectID],
    firstName: String,
    lastName: String,
    loc: Point)

case class Point(lon: Double, lat: Double)

object Point {
    implicit object PointWriter extends BSONDocumentWriter[Point] {
        def write(point: Point): BSONDocument = BSONDocument(
            "type" -> "Point",
            "coordinates" -> Seq(point.lat, point.lon)
        )
    }

    implicit object PointReader extends BSONReader[BSONDocument, Point] {
        def read(doc: BSONDocument): Point = Point(88, 88)
    }
}

object JsonFormats {
    import play.api.libs.json.Json
    import play.api.data._
    import play.api.data.Forms._

    import play.api.libs.json._
    import play.api.libs.functional.syntax._

    implicit val pointFormat = Json.format[Point]
}

当我调用控制器操作时createCC,我希望新创建的文档有一个格式正确的 Point 对象,但我实际得到的是这样的:

{
    "_id": ObjectId("52ac76dd1454bbf6d96ad1f1"),
    "loc": {
        "lon": -0.0015,
        "lat": 51.0015 
    }
}

所以我尝试使用PointWriterPointReader告诉 ReactiveMongo 如何将这样的Point对象写入数据库根本没有效果。

谁能帮我理解我必须做什么?

(我来自 PHP 背景,并试图让我的头脑围绕 Scala ......)

更新: 感谢 tmbo 的回答,我想出了这位作家:

val pointWrites = Writes[Point]( p =>
    Json.obj(
        "type" -> JsString("Point"),
        "coordinates" -> Json.arr(JsNumber(p.lon), JsNumber(p.lat))
    )
)
4

1 回答 1

5

您面临的问题与 和 之间的混淆JSONCollection有关BSONCollection

BSONCollection是reactivemongo使用的默认集合。此实现需要实现 aBSONDocumentWriter和 aBSONReader以使案例类获得(反)序列化。

JSONCollection另一方面是play-reactive 模块使用的默认集合实现。由于您将集合定义为JSONCollectionin,因此db.collection[JSONCollection]("test")您需要提供隐式 json 格式。

您提供的 json 格式是

implicit val pointFormat = Json.format[Point]

这会将对象序列化为以下格式

{
    "lon": -0.0015,
    "lat": 51.0015 
}

如果你想序列化你Point的数组,你需要替换上面的隐式pointFormat

import play.api.libs.json._
import play.api.libs.json.Reads._

case class Point(lng: Double, lat: Double)

object Point {

  val pointWrites = Writes[Point]( p => Json.toJson(List(p.lng, p.lat)))

  val pointReads = minLength[List[Double]](2).map(l => Point(l(0), l(1)))

  implicit val pointFormat = Format(pointReads, pointWrites)
}

你实际上不需要BSONReaderand BSONDocumentWriter

编辑: 这是一个也验证type文档属性的读取:

val pointReads =
  (__ \ 'type).read[String](constraints.verifying[String](_ == "Point")) andKeep
    (__ \ 'coordinates).read[Point](minLength[List[Double]](2).map(l => Point(l(0), l(1))))
于 2013-12-17T09:00:33.673 回答