【问题标题】:Error on a count query on mongodb using reactivemongo使用reactivemongo对mongodb进行计数查询时出错
【发布时间】:2014-11-28 13:03:01
【问题描述】:

我正在编写一个 play 2.3.2 应用程序。 在我的应用程序中,我使用 MongoDB 数据库。 我有一个 Recommendation.tags 和 Recommendation.request 集合。 它们具有以下 JSON 格式: 1) 推荐标签:

 {
    "_id" : ObjectId("542e65fb7ab45a4189944137"),
    "tag" : "Meat:Pork - Bacon Cooked Slcd"
}

2) 推荐.请求

{
    "_id" : ObjectId("542e67e07f724fc2af28ba74"),
    "id" : "6649fd2b-c616-4693-aec5-a2a2a1658417",
    "user" : {
        "id" : "",
        "email" : "alberto@gmail.com"
    },
    "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

怎么了??

@编辑

[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] 并在相同的情况下对其进行迭代??

【问题讨论】:

  • Json.obj("tags.tag" : tag.category + " " + tag.name) 不应该这样贴花:Json.obj("tags.tag" -&gt; tag.category + ":" + tag.name) 吗?
  • 是的,但是编译器继续给我错误,看我的@edit
  • tags是Future with List,然后tag &lt;- tags给你Future里面的list(Future也是Collection),改用tag &lt;- tags.flatMap
  • 你能发布一些代码吗?它不编译..
  • 抱歉打扰了,我在描述Future 时想到了Option。但问题是一样的,你尝试用里面的 List 迭代 Future。

标签: mongodb scala future reactivemongo


【解决方案1】:

解决了

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
                                                    }

                            }

                        }
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多