使用 ReactiveMongo 的嵌套查询
Nested queries with 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
}
}
我不确定在这里做什么。如果文章被保存或更新,代码应该 return Future(true),否则为 false。我在这里错过了 reactivemongo and/or scala 期货的一些东西。
使用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)
}
}
我有一个文章集,其中字段 "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
}
}
我不确定在这里做什么。如果文章被保存或更新,代码应该 return Future(true),否则为 false。我在这里错过了 reactivemongo and/or scala 期货的一些东西。
使用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)
}
}