在 ReactiveMongo 中获取集合的惯用方式

编程入门 行业动态 更新时间:2024-10-26 07:25:50
本文介绍了在 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 中获取集合的惯用方式

本文发布于:2023-11-30 10:42:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   ReactiveMongo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!