3

我有一个 mongo 条目,其中一个字段是列表。如果条目不存在,我想添加一个新条目。如果存在,我想将一个新元素附加到列表中。

最后我想向客户端返回一个 Ok,但只有在操作成功完成之后。不是一个严格的要求,但在我看来,这对用户来说是最有意义的。

这就是我目前所拥有的 - 它可以工作,但在更新时它会覆盖旧列表,而不是附加新元素。

def myMethod(value:String, value2:String) = Action {

  Async {
    val myElement = Json.obj(
      "key" -> value2
    )

    val myDBEntry = Json.obj(
      "key" -> value,
      "list" -> List(myElement)
    )

    collection.update(Json.obj("key" -> value), myDBEntry, upsert = true).map(lastError =>
      Ok("Mongo LastError: %s".format(lastError)))
  }
}

为了检查列表是否存在并附加元素/创建一个新列表,我从类似的东西开始(这替换了collection.update前面代码中的块):

val futureResult:Future[Option[JsObject]] = collection.find(Json.obj("key" -> value)).one[JsObject]

futureResult.map { result =>

  if (result.isEmpty) {
      collection.insert(myDBEntry).map(lastError =>
        Ok("Mongo LastError: %s".format(lastError)))


  } else {
      //this not the correct command yet - but compiler already fails because method is not returning correct future 
      collection.update(Json.obj("key" -> value), myDBEntry, upsert = true).map(lastError =>
        Ok("Mongo LastError: %s".format(lastError)))
  }


}

但编译器似乎不喜欢这种嵌套:“类型不匹配,预期:Future[Result],实际:Future:[Future[SimpleResult[Nothing]]]”

无论如何我觉得这种方式有点尴尬,必须有更优雅的方式来解决这个问题,但我是 Futures 和 ReactiveMongo 的新手,不知道。我该如何解决?

编辑:我也找到了这篇文章但我认为这是在数据库操作完成之前返回响应,我不希望这样。

4

1 回答 1

4

尝试更改此行:

futureResult.map { result =>

对此:

futureResult.flatMap { result =>

当您在未来执行地图并且在该地图块内返回另一个未来时,您需要使用 flatMap 代替,因为它会使嵌套变平。

于 2013-08-13T10:58:09.170 回答