【发布时间】:2015-11-29 17:51:15
【问题描述】:
给定以下类型
type GeoLocation = (Double, Double)
我想把它存储在我的数据库中
location: [-55.23, 123.7]
此外,位置数据是可选的,因此 API 公开了Option[GeoLocation]。当需要存储数据时,我会对其进行转换。
val coordinates: Option[GeoLocation] = ...
val location = coordinates match {
case Some((lng, lat)) => Some(lng :: lat :: Nil)
case None => None
}
这样我就可以选择将它添加到包含的文档中。
location.map(doc.put("location", _))
当我想将 if 从数据库对象转换回 GeoLocation 时,我做了这件令人讨厌的事情......
val coordinates = dbo.getAs[MongoDBList]("location").map(_.toList.map(_.asInstanceOf[Double])) match {
case Some(List(lng, lat)) => Some(lng, lat)
case None => None
}
在我看来,在 MongoDB 中将元组存储为数组有很多仪式。有没有更有效、更直接的方法来实现这一点?
【问题讨论】:
-
有没有更好的答案?