使用JsObject计算Reactivemongo

时间:2013-08-29 11:00:03

标签: mongodb scala playframework-2.1 reactivemongo

我正在尝试在查询上实现Count但是我遇到了问题,因为我没有 使用BSONDocument但使用JsObject对象。

https://github.com/ReactiveMongo/ReactiveMongo/blob/master/driver/samples/SimpleUseCasesSample.scala

我应该将我的JsObject翻译成BSONDocument还是有更好的方法? 你会如何在ReactiveMongo 0.9中做到这一点?

1 个答案:

答案 0 :(得分:2)

翻译示例:

<强>服务

  def countSentMailForVenue(currentId: Long, from: DateTime, to: DateTime) : Future[Int] = {
    val query = Json.obj("venueInfo.currentId" -> venueId, "origin" -> Origin.EMAIL_INVITE, "updated" -> Json.obj("$gte" -> from.getMillis, "$lt" -> to.getMillis))
    count(BSONFormats.toBSON(query).get.asInstanceOf[BSONDocument])
  }

在道:

/**
   * Currently Count() only supports BSONDocument.
   */
  def count(query: BSONDocument) : Future[Int] = {
    Logger.debug(s"Counting documents: "+BSONFormats.toJSON(query))
    val futureCount = collection.db.command(
      Count(
        collection.name,
        Some(query)
      )
    )
    futureCount
  }