0

我正在测试 mongo shell 上的下一个 2 查询,它可以工作,

但是现在,我需要在 reactivemongo 中执行相同的查询

有人可以给我一个关于如何在reactivemongo中进行查询的建议

doc = db.offer.find({"_id": "5704441ea356f55ab590e8f4"})

db.student.update(
  { "_id" : "570681b30fc032dea831c132"},
  { $push: { 
    "presell": [
        { "_id" : doc }
      ]
    } 
  }
)

有没有更好的方法来运行这个查询?

4

1 回答 1

0

使用 flatMap 是我正在寻找的解决方案

  def preSell( user_id: String, offer_id: String ) = Action.async {

    val futureResults = collection_offer.find( Json.obj("_id" -> offer_id ) ).one[JsValue]
    futureResults.flatMap {
      case Some(document) => 

        val futureUpdate = collection.update( Json.obj( "_id" -> user_id ), Json.obj( "$addToSet" -> Json.obj( "presell" ->  Json.toJson(document) ) ) )

        futureUpdate.map { result =>
          Logger.debug("Successfully update")
          Ok( Json.obj( data -> Json.obj( "_id" -> user_id ) ) )
        }.recover {
          case t: TimeoutException =>
            Logger.error("Problem found in student update process")
            InternalServerError(t.getMessage)
        }

      case None => 
        Future.successful( Ok( Json.obj( data -> "Document NotFound" ) ) )
    }.recover {
      case t: TimeoutException =>
        Logger.error("Problem obtaining teacher")
        InternalServerError(t.getMessage)
    }

  }

Scala Play Action.async 无法将 Ok 解析为 mvc.AnyContent

于 2016-04-11T13:48:46.010 回答