使用ReactiveMongo嵌套查询

时间:2015-04-25 13:44:27

标签: mongodb scala reactivemongo

我有一个文章集合,其中“title”和“publication”字段具有唯一的组合键约束。

当调用insertOrUpdateArticle(a:Article)时,它会首先尝试插入它,如果遇到约束,它应该更新文章 - 如果需要的话。

然而,在那之前我就被困住了。目前的错误是:

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

来源:

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
  }
}

我不确定该怎么做。如果文章已保存或更新,代码应返回Future(true),否则返回false。我在这里错过了有关反应性和/或斯卡拉期货的东西。

1 个答案:

答案 0 :(得分:0)

使用recoverWith创建新的Future是解决方案,修改后的代码:

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