我正在尝试使用ReactiveMongo中的Aggregate,但是我遇到了一个奇怪的错误。
我创建了一个像这样的聚合命令:
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))
))
打电话给mongo:
val result = collection.db.command(command)
这将返回成功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>)))
然后我尝试将流转换为BSONDocument:
result.map { doc =>
val bson = doc.asInstanceOf[BSONDocument]
Logger.info(BSONDocument.pretty(bson))
bson
}
但我得到了:
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
要解决这个问题,让我们使用Json lib:
val doc = result.map { Json.toJson(_) }
这让我:
[
{
"_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
}
]
成功! 所以我的问题是。我应该如何将我的流转换为BSONDocument而不是json值?
答案 0 :(得分:1)
您的问题是,您的结果不是Stream[BSONDocument]
,而是Success[ErrorType, Stream[BSONDocument]]
。
所以你的result.map
在成功上做地图(我打赌这是scalaz.Success?)而不是在Stream上。因此,地图函数中的doc
变量为Stream[BSONDocument]
而非BSONDocument
(显然您无法将流转换为BSONDocument)
如果您想访问BSONDocuments,您必须执行以下操作:
result match {
case Failure(error) => throw new Exception("") // handle the error somehow
case Success(stream) => stream map { doc => ... }
}
重要的是要知道Stream是一个集合,所以你有多个BSONDocuments,如果你想将它折叠成一个BSONDocument对象,你需要进行折叠操作之一(fold,foldLeft ,foldRight)的流。
有关更多信息,请参阅:http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Stream