我正在编写一个play 2.3.2应用程序。在我的应用程序中,我使用MongoDB数据库。我有一个requestion.tags和recommendation.request集合。它们具有以下JSON格式:1)Recommendation.tags:
{
"_id" : ObjectId("542e65fb7ab45a4189944137"),
"tag" : "Meat:Pork - Bacon Cooked Slcd"
}
2)Recommendation.requests
{
"_id" : ObjectId("542e67e07f724fc2af28ba74"),
"id" : "6649fd2b-c616-4693-aec5-a2a2a1658417",
"user" : {
"id" : "",
"email" : "[email protected]"
},
"tags" : [
{
"tag" : "Fish:Swordfish Loin Portions"
},
{
"tag" : "Vegetable:Carrots - Jumbo"
}
],
"date" : 1412327392380
}
我正在编写一个处理所有统计信息请求的控制器。在这种情况下,我正在编写一种搜索系统中最常用标签的方法。为此,我将反应式mongo驱动程序用于scala。这是使用的代码:
/**
* Method that search the most used tag.
*/
def max = Action {
var max = 0
var tag = null
val tags: Future[List[Tag]] = Tags.find(Json.obj()).toList
for{
tag <- tags
tagsOk <- Requests.find(Json.obj("tags.tag" -> tag.category + " " + tag.name)).count
if(tagsOk > max) {
max = tagsOk
tag = tag.category + " " + tag.name //string tag
}
}
Ok(tag)
}
但是编译器给我以下错误:
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/controllers/manager/StatisticsController.scala:28: identifier expected but string literal found.
[error] tagsOk <- Requests.find(Json.obj("tag" : tag.category + " " + tag.name)).count
[error] ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/controllers/manager/StatisticsController.scala:33: ')' expected but '}' found.
[error] }
[error] ^
[error] two errors found
[error] (compile:compile) Compilation failed
怎么了??
@编辑
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/controllers/manager/StatisticsController.scala:28: value category is not a member of List[recommendationsystem.models.Tag]
[error] tagsOk <- Requests.find(Json.obj("tags.tag" -> tag.category + " " + tag.name)).count
[error]
@newedit
我已经解决了使用以下代码:
val tags = for{
tags <- futureTags
}
for(document <- tags) {
val tagsOk = Requests.find(Json.obj("tags.tag" -> document.category))
}
有没有一种方法可以获取List [T]并对其进行相同的迭代?
解决使用
def max = Action {
var max: Int = 0
var tagFound: Tag = null
//obtain all the tags in the db.
val futureTags: Future[List[Tag]] = Tags.all.toList
futureTags map{ (tags: List[Tag]) =>
tags map {
(tag: Tag) =>
//create the tag String
val tagName = tag.category + ":" + tag.attr
//search the documents where tags.tag == tag in the db.
val futureRequests : Future[List[recommendationsystem.models.Request]]= Requests.find(Json.obj("tags.tag" -> tagName)).toList
futureRequests map { (requests: List[recommendationsystem.models.Request]) =>
//get the numbers of documents matching the tag
val number = requests.size
if(number > max) {
max = number
tagFound = tag
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句