Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何将Future[b条件文档]转换为list?_Mongodb_Scala_Reactivemongo - Fatal编程技术网

Mongodb 如何将Future[b条件文档]转换为list?

Mongodb 如何将Future[b条件文档]转换为list?,mongodb,scala,reactivemongo,Mongodb,Scala,Reactivemongo,代码使用ReactiveMongo向MongoDB发送请求,并返回Future[BSONDocument]但我的代码处理数据列表,因此我需要获取Future[BSONDocument]的值,然后将其转换为列表 我如何在没有阻塞的情况下更好地执行该操作 Upadte: 我正在使用ReactiveMongoRawCommand def findLByDistance()(implicit ec: ExecutionContext) = db.command(RawCommand(

代码使用ReactiveMongo向MongoDB发送请求,并返回
Future[BSONDocument]
但我的代码处理数据列表,因此我需要获取
Future[BSONDocument]
的值,然后将其转换为列表

我如何在没有阻塞的情况下更好地执行该操作

Upadte:

我正在使用ReactiveMongo
RawCommand

 def findLByDistance()(implicit ec: ExecutionContext) =  db.command(RawCommand(
        BSONDocument(
            "aggregate" -> collName,
            "pipeline" -> BSONArray(BSONDocument(
                "$geoNear" -> BSONDocument(
                    "near" -> BSONArray(44.72,25.365),
                    "distanceField" -> "location.distance",
                    "maxDistance" -> 0.08,
                    "uniqueDocs" -> true)))
                )))
结果将在
Future[BSONDocument]
中显示。对于一些简单的查询,我使用默认的查询生成器,它允许简单的转换

def findLimitedEvents()(implicit ec: ExecutionContext) =
  collection.find(BSONDocument.empty)
    .query(BSONDocument("tags" -> "lazy"))
    .options(QueryOpts().batchSize(10))
    .cursor.collect[List](10, true)
我基本上需要
RawCommand
输出类型来匹配以前使用的。

如果不阻塞,就无法“获得”未来。如果要等待
未来的
完成,则必须阻止

您可以做的是将一个
未来
映射到另一个
未来

val futureDoc: Future[BSONDocument] = ...
val futureList = futureDoc map { doc => docToList(doc) }
最终,你会到达一个点,你已经组合、映射、恢复了你所有的未来,并希望结果发生一些事情。这是您阻止或建立处理程序以处理最终结果的地方:

val futureThing: Future[Thing] = ...

//this next bit will be executed at some later time,
//probably on a different thread
futureThing onSuccess {
  case thing => doWhateverWith(thing)
}

不确定您的确切用例(显示更多代码会有所帮助),但将
List[Future[BSONDocument]]
转换为一个
Future[List[BSONDocument]]
,这样您就可以更容易地
onSuccess
map
了,您可以通过:

val futures: List[Future[A]] = List(f1, f2, f3)
val futureOfThose: Future[List[A]] = Future.sequence(futures)

您需要什么的列表?对于BSONDocuments,返回的实际数据结构应该如下所示[list[BSONDocument]]在其他情况下,我可以访问
cursor
,因此转换看起来像
db.collection.find(BSONDocument()).cursor.collect[list]()
如果您询问如何将bSondDocument转换为列表,或者如何从未来获取计算值,则不清楚。此外,显示代码可能会有所帮助。@vptheron两者都有,但由于我之前已将BSONDocument转换为列表,所以我认为应该是类似的