如何使用Salat搜索存储在元组中的数据?

时间:2014-10-14 23:49:14

标签: mongodb scala salat

case class Venue(@Key("_id") id: Int, 
                 location: Tuple2[Double, Double],
                 name: String)

object VenueDAO extends SalatDAO[Venue, Int](collection = MongoConnection()("ec")("venue"))

VenueDAO.find(?) //return Option[Venue]

如何使用Salat按位置搜索数据?

1 个答案:

答案 0 :(得分:1)

开始的两件事:

  1. 根据README.txt,Salat不支持元组。
  2. 也许你在想Mongo的Geospatial coordinate support
  3. 无论如何,由于Salat不支持Tuples,所以你可以做到:

    case class Location(x: Double, y: Double)
    
    case class Venue(@Key("_id") id: Int, location: Location, name, String)
    
    val venue = Venue(1, Location(1.0, 1.0), "NYC")
    VenueDAO.save(venue)   
    println(s"Saved: $venue")
    val found = VenueDAO.findOne(MongoDBObject("location.x" -> 1.0, "location.y" -> 1.0))
    println(s"Found: $found")
    

    打印:

    Saved: Venue(1,Location(1.0,1.0),NYC)
    Found: Some(Venue(1,Location(1.0,1.0),NYC))