Search code examples
mongodbscalasalat

How to search data stored in tuples using 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]

How to search data by location using Salat?


Solution

  • Two things to start out:

    1. Per the README.txt, Salat doesn't support Tuples.
    2. Perhaps you are thinking of Mongo's Geospatial coordinate support?

    In any case, since Salat doesn't support Tuples, here is what you can do:

    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")
    

    Prints:

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