我有一个mongo集合,如下所示:
{ "_id" : ObjectId("456391bf2db8613231935362"), "country" : "denmark", "city" : "copenhagen" }
{ "_id" : ObjectId("456391bf2db8613231935363"), "country" : "denmark", "city" : "odense" }
{ "_id" : ObjectId("456391bf2db8613231935364"), "country" : "austria", "city" : "vienna" }
{ "_id" : ObjectId("456391bf2db8613231935365"), "country" : "austria", "city" : "vilach" }
...
我想创建一个查找方法,将一个国家/地区的所有城市都作为地图找到,从以下内容开始:
def findAllCittiesPerCountry(): Future[Map[String,List[String]]] = {
myCollection.find.map(obj => obj.get("country").get).toFuture().map(x => x.distinct)
}
但它不是解决方案,太笨拙而且不够干净......
有人有建议吗?
谢谢!