0
我正在寫一個play 2.3.2應用程序。 在我的應用程序中,我使用了一個MongoDB數據庫。 我有一個recommendation.tags和recommendation.request集合。 他們具有以下JSON格式: 1)recommendation.tags:使用reactivemongo對mongodb進行計數查詢的錯誤
{
"_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
}
我正在寫處理所有的統計數據請求的控制器。 在這種情況下,我正在寫一個方法來搜索系統中最常用的標籤。 爲了做到這一點,我使用了scala的反應式mongo驅動程序。 這是所使用的代碼:
/**
* 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
有什麼不對?
@edit
[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))
}
有沒有辦法讓名單[T]並在其上迭代後在相同的?
解決呢'Json.obj( 「tags.tag」:tag.category + 「」 + tag.name)'不應該decalred這樣說:'Json.obj(」 tags.tag「 - > tag.category +」:「+ tag.name)'? – 2014-10-03 13:41:13
是的,但編譯器繼續給我錯誤,看到我的@edit – 2014-10-03 13:47:31
'標籤'是未來的列表,然後'標籤< - 標籤'給你列表裏面的未來(未來也是集合),使用'標籤< - tags.flatMap'而不是 – 2014-10-03 13:56:45