0

I'm trying to make use of the Aggregate in ReactiveMongo but I'm getting a strange error.

I create a Aggregate command like this:

val command = Aggregate("invites", Seq(
      Match(BSONDocument("origin" -> 1, "status" -> 3, "created" -> BSONDocument("$gte" -> from.getMillis, "$lt" -> to.getMillis))),
      Sort(List(reactivemongo.core.commands.Ascending("_id"))),
      Group(BSONDocument("myId" -> "$venues.myId", "name" -> "$venues.name"))("count" -> SumValue(1))
    ))

call mongo:

val result = collection.db.command(command)

this returns a success Stream:

Success(Stream(BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>), BSONDocument(<non-empty>)))

I then try to cast the stream to a BSONDocument:

result.map { doc =>
      val bson = doc.asInstanceOf[BSONDocument]
      Logger.info(BSONDocument.pretty(bson))
      bson
    }

But I'm getting:

    Failure(java.lang.ClassCastException: scala.collection.immutable.Stream$Cons cannot be cast to reactivemongo.bson.BSONDocument)

scala.collection.immutable.Stream$Cons cannot be cast to reactivemongo.bson.BSONDocument

To go around this issue, let's make use of Json lib:

val doc = result.map { Json.toJson(_) }

That gives me:

    [
    {
        "_id": {
            "myId": [
                3111669
            ],
            "name": [
                "Some cool name 1"
            ]
        },
        "count": 3
    },
    {
        "_id": {
            "myId": [
                3091695
            ],
            "name": [
                "Some cool name 2"
            ]
        },
        "count": 19
    },
    {
        "_id": {
            "myId": [
                896
            ],
            "name": [
                "Coole name"
            ]
        },
        "count": 1
    },
    {
        "_id": {
            "myId": [
                933
            ],
            "name": [
                "BALBLABLBL"
            ]
        },
        "count": 1
    },
    {
        "_id": {
            "myId": [
                3000831
            ],
            "name": [
                "Cleaning Services"
            ]
        },
        "count": 2
    },
    {
        "_id": {
            "myId": [
                3389731
            ],
            "name": [
                "Car company number uno"
            ]
        },
        "count": 5
    }
]

Success! So my question is. How should I translate my stream to a BSONDocument instead of a json value?

jakob
  • 5,979
  • 7
  • 64
  • 103

1 Answers1

1

Your problem is, that your result isn't a Stream[BSONDocument], but a Success[ErrorType, Stream[BSONDocument]].

So your result.map does the map on Success (i bet this is scalaz.Success?) and not on the Stream. Because of this your doc variable in the map function is Stream[BSONDocument] and NOT BSONDocument (and obviously you can't cast a Stream to a BSONDocument)

If you want to access the BSONDocuments, you have to do something like this:

result match {
  case Failure(error) => throw new Exception("") // handle the error somehow
  case Success(stream) => stream map { doc => ... }
}

Important to know is that Stream is a collection, so you have multiple BSONDocuments in it and if you want to fold it into one BSONDocument object you need to one of the fold operations (fold, foldLeft, foldRight) of the Stream.

For further informations see: http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Stream

regexp
  • 766
  • 4
  • 14