在 ReactiveMongo 中获取集合的惯用方式 [英] Idiomatic way of getting a collection in ReactiveMongo

查看:34
本文介绍了在 ReactiveMongo 中获取集合的惯用方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Play 2.5.4 和 ReactiveMongo 编写一个 Play/Scala 应用程序.基于这个例子,我m 使用

I'm writing a Play/Scala application using Play 2.5.4 and ReactiveMongo. Based on this example, I'm getting the collection using

class SettingStore( val mongo:ReactiveMongoApi) {
  def collection = mongo.db.collection[BSONCollection]("Settings")
  // more code...
}

然而,db 现在已被弃用.弃用警告建议我使用 database,但这个返回一个 Future,因此所有操作都必须映射.由于 ReactiveMongo 的操作也返回 Futures,这似乎是多余的......获取集合的正确方法是什么?(或者,我是否完全遗漏了某些东西而 Future[Future[Future[A]]] 是未来?)

However, db is now deprecated. The deprecation warning recommends I use database, but this one returns a Future so all operations have to be mapped over. Since ReactiveMongo's operations also return Futures, this seems redundant... what's to proper way of getting a collection? (or, am I missing something completely and Future[Future[Future[A]]] is the future?)

推荐答案

如果你看看 文档,您可以看到使用 .database 函数的示例,而不是已弃用的 .db.

If you have a look at the documentation, you can see examples using the .database function, instead of the deprecated .db.

非异步 .db 已被弃用,因为它没有提供足够的保证来在 MongoConnection 池中找到活动连接.

The non-async .db has been deprecated as it didn't offer sufficient guaranty to be able to find an active connection in the MongoConnection pool.

假设池一启动就至少有一个连接处于活动状态,但情况并非总是如此,因为根据网络速度/延迟,检查/发现 ReplicaSet 节点可能需要时间.

It was assuming at least one connection is active as soon as the pool is started, which is not always the case as checking/discovering the ReplicaSet nodes can take time, according the network speed/latency.

如果驱动程序在一段时间内无法加入节点(网络中断、节点重新启动等),则相同的断言可能是错误的.可能需要一些时间,以便节点表明它们已重新上线.

The same assertion can be wrong in case the driver cannot join the nodes for some time (network interruption, nodes restarted, ...). It can take some time so the nodes indicate they are back on line.

新的 .database 解析是异步的,并使用 FailoverStrategy 等待(或不)可用连接(根据选择的读取首选项,...).

The new .database resolution is async, and use a FailoverStrategy to wait (or not) for an available connection (according the chosen read preference, ...).

这篇关于在 ReactiveMongo 中获取集合的惯用方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆