Search code examples
mongodbscalareactivemongo

Nested queries with ReactiveMongo


I have an article collection where fields "title" and "publication" has a unique combined key constraint.

When calling insertOrUpdateArticle(a: Article) it will first try to insert it, in case it hits the constraint, it should update the article - if needed.

However, I'm stuck before that. Current error is:

Error:(88, 57) type mismatch;
 found   : scala.concurrent.Future[scala.concurrent.Future[Boolean]]
 required: Boolean
            col_article.find(selector).one[Article].map {

Source:

def insertOrUpdateArticle(a: Article): Future[Boolean] = {
  // try insert article
  col_article.insert[Article](a).map {
    // article inserted
    lastError => {
      println("Article added.")
      true
    }
  }.recover {
    case lastError: LastError =>
      // check if article existed
      lastError.code.get match {
        case 11000 => {
          // article existed (duplicate key error)

          // load old article
          val selector = BSONDocument(
            "title" -> BSONString(a.title),
            "publication" -> BSONString(a.publication)
          )

          col_article.find(selector).one[Article].map {
            case Some(old_a: Article) => {
              // TODO: compare a with old_a
              // TODO: if a differs from old_a, update
              Future(true)
            }
            case None => {
              // something went wrong
              Future(false)
            }
          }
        }
        case _ => {
          println("DB.insertOrUpdateArticle() unexpected error code when inserting: " + lastError.code.get)
          false
        }
      }
    case ex =>
      println("DB.insertOrUpdateArticle() unexpected exception when inserting: " + ex)
      false
  }
}

I'm unsure what to do here. The code should return Future(true) if the article was saved or updated, false otherwise. There's something with reactivemongo and/or scala futures I'm missing out here.


Solution

  • Using recoverWith to create new Future is the solution, modified code:

    def insertOrUpdateArticleOld(a: Article): Future[Boolean] = {
      // try insert article
      col_article.insert[Article](a).map {
        // article inserted
        lastError => {
          println("Article added.")
          true
        }
      }.recoverWith {
        case lastError: LastError =>
          // check if article existed
          lastError.code.get match {
            case 11000 => {
              // article existed (duplicate key error)
    
              // load old article
              val selector = BSONDocument(
                "title" -> BSONString(a.title),
                "publication" -> BSONString(a.publication)
              )
    
              col_article.find(selector).one[Article].flatMap {
                case Some(old_a: Article) => {
                  // TODO: compare a with old_a
                  // TODO: if a differs from old_a, update
                  Future(true)
                }
                case None => {
                  // something went wrong
                  Future(false)
                }
              }
            }
            case _ => {
              println("DB.insertOrUpdateArticle() unexpected error code when inserting: " + lastError.code.get)
              Future(false)
            }
          }
        case ex =>
          println("DB.insertOrUpdateArticle() unexpected exception when inserting: " + ex)
          Future(false)
      }
    }