Frage

I'm trying to implement Count on a query but I'm having issues since I do not work with BSONDocument but with JsObject objects.

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

Should I translate my JsObject to a BSONDocument or is there a better way? How would you do this in ReactiveMongo 0.9?

War es hilfreich?

Lösung

Translate example:

Service:

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

And in Dao:

/**
   * 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
  }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top