0

i'm writing a play 2.3.2 application. In my application i use a MongoDB database. I've a recommendation.tags and recommendation.request collections. They has the following JSON format: 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" : "alberto@gmail.com"
    },
    "tags" : [
        {
            "tag" : "Fish:Swordfish Loin Portions"
        },
        {
            "tag" : "Vegetable:Carrots - Jumbo"
        }
    ],
    "date" : 1412327392380
}

I'm writing a Controller that handle all the statistics request. In this case i'm writing a method that search the most used tag in the system. For do that i'm using the reactive mongo driver for scala. This is the code used:

/**
   * 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)
 }

But the compiler give me the following errors:

[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

What's wrong??

@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

I've solved using this code:

val tags = for{
         tags <- futureTags
       }
       for(document <- tags) {
         val tagsOk = Requests.find(Json.obj("tags.tag" -> document.category))

       }

Is there a way to get the List[T] and after iterate on it in the same for??

alberto adami
  • 729
  • 1
  • 6
  • 25

1 Answers1

0

Solved using

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
                                                    }

                            }

                        }
    } 
alberto adami
  • 729
  • 1
  • 6
  • 25