我已经开始通过创建一个简单的Scala项目来学习reactivemongo.我从SBT开始.这是我的build.sbt档案:
name := "mongo-test"
version := "1.0"
scalaVersion := "2.10.2"
libraryDependencies ++= Seq(
"org.reactivemongo" %% "reactivemongo" % "0.9"
)
Run Code Online (Sandbox Code Playgroud)
但是我在执行compile命令时遇到错误:
[info] Resolving play#play-iteratees_2.10;2.1.0 ...
[warn] module not found: play#play-iteratees_2.10;2.1.0
[warn] ==== local: tried
[warn] /home/amir/.ivy2/local/play/play-iteratees_2.10/2.1.0/ivys/ivy.xml
[warn] ==== public: tried
[warn] http://repo1.maven.org/maven2/play/play-iteratees_2.10/2.1.0/play-iteratees_2.10-2.1.0.pom
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: play#play-iteratees_2.10;2.1.0: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
Run Code Online (Sandbox Code Playgroud)
更新:我的SBT版本是0.13.0.
我已经安装了以下内容:1.Play 2.4 2.Created一个scala项目3.添加了eclipse插件
现在我想添加一个数据库连接.我想试用ReactiveMongo,但维基页面上的说明适用于2.3或更早版本.
https://github.com/ReactiveMongo/Play-ReactiveMongo
对于2.4,似乎游戏的文件结构已经改变.我需要知道为ReactiveMongo配置play 2.4的正确方法.
以下是他们为2.4以上的游戏版本提供的说明:
If you want to use the latest snapshot, add the following instead (only for play > 2.3):
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0-SNAPSHOT"
)
Configure your application to use ReactiveMongo plugin
add to your conf/play.plugins
1100:play.modules.reactivemongo.ReactiveMongoPlugin
Configure your database access within application.conf
Run Code Online (Sandbox Code Playgroud)
如何将配置应用于play 2.4的新文件结构?
这是我试图做的没有成功:在项目/ plugins.sbt我添加:
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
addSbtPlugin("org.reactivemongo" % "play2-reactivemongo" % "0.11.0-SNAPSHOT")
Run Code Online (Sandbox Code Playgroud)
我得到一个解决错误消息:
at java.lang.Thread.run(Thread.java:745)
[error] (*:update) sbt.ResolveException: unresolved …Run Code Online (Sandbox Code Playgroud) 我正在用akka-http和ReactiveMongo编写一个Web服务.我遇到了问题,我无法自己解决.
我有方法
def saveRoute(route: Route)(implicit writer: BSONDocumentWriter[Route]): Future[WriteResult] = {
collection.insert(route)
}
Run Code Online (Sandbox Code Playgroud)
问题是WriteResult除了错误或OK状态之外不包含任何有用的信息.
你能解释一下如何在插入后获取插入的对象ID.我发现的所有示例都LastError与Play 版本的旧版本有关!框架.
我们的一个生产系统存在问题,并且我们一生无法找到解决方案
2017-08-22 19:15:52.337 [Supervisor-1 / Connection-2]遇到错误,不再需要尝试。正在完成失败... reactmongo.core.actors.Exceptions $ ChannelNotFound:MongoError ['从主节点找不到通道:'{{cluster-shard-0}}。mongodb.net:27017'{authenticated:0,已连接:10,通道:10}(Supervisor-1 / Connection-2)']由以下原因引起:reactmongo.core.actors.Exceptions $ InternalState:reactmongo.ConnectAll $ IsMaster(785,{{NodeSet None Node [{{ cluster-shard-0}}。mongodb.net:27017:主节点(10/10个可用连接),延迟= 5],auth = Set()| Node [{{cluster-shard-1}}。mongodb.net: 27017:辅助节点(10/10个可用连接),延迟= 5],auth = Set(Authenticated(admin,{{collection}}))| Node [{{cluster-shard-2}} .. mongodb.net:27017:次要(10/10个可用连接),延迟= 5],{{collection}}))| | 节点[{{cluster-shard-2}}。mongodb.net:27017:辅助节点(10/10可用连接),延迟= 2],auth = Set(Authenticated(admin,{{collection}}))}}( )在reactmongo.IsMaster(784,{{NodeSet None Node [{{cluster-shard-0}} .. mongodb.net:27017:Primary(10/10可用连接),延迟= 2],auth = Set()| Node [{{cluster-shard-1}}。mongodb.net:27017:辅助节点(10/10可用连接,延迟= 1),auth = Set(Authenticated(admin,{{collection}}))| Node [ {{cluster-shard-2}}。mongodb.net:27017:辅助(10/10可用连接),延迟= 2],auth = Set(Authenticated(admin,{{collection}}))}}()在reactmongo.RefreshAll({{NodeSet None Node [{{cluster-shard-0}}。mongodb.net:27017:主节点(10/10可用连接),延迟= 2],auth = Set()| Node [{{ cluster-shard-1}}。mongodb.net:27017:辅助服务器(10/10个可用的连接),等待时间= 1],auth = Set(Authenticated(admin,{{collection}}))| 节点[{{cluster-shard-2}}。mongodb.net:27017:辅助节点(10/10可用连接),延迟= 2],auth = Set(Authenticated(admin,{{collection}})}}}) ()在reactmongo.ConnectAll({{NodeSet None Node [{{cluster-shard-0}}。mongodb.net:27017:Primary(10/10可用连接),delay = … 如何在 ReactiveMongo 中编写以下查询。
db.products.insert(
{ item: "envelopes", qty : 100, type: "Clasp" },
{ writeConcern: { w: 2, wtimeout: 5000 } }
)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过类似下面的方法。
db.products.insert(BSONDocument("item"->"envelopes","qty " -> 100,"type" -> "Clasp"))
Run Code Online (Sandbox Code Playgroud)
您能否在这里建议我如何在 ReactiveMongo 中编写带有写入问题的插入查询。
我在 DB 连接中使用了 writeConcern 大多数,例如
val conOpts = MongoConnectionOptions(authMode = ScramSha1Authentication, authSource = Some(Configuration.dbName), writeConcern = WriteConcern.Default.copy(w = WriteConcern.Majority))
Run Code Online (Sandbox Code Playgroud) 我们最近升级到了Play 2.7.0,并使用play2-reactivemongo版本0.16.2和reactmongo 0.16.3。我们使用reactivemongo-shaded-native但也尝试不使用。
我们正在连接到具有3个节点的副本集,即MongoDB Atlas上的MongoDB 3.6.10。
初始连接良好,服务运行了一段时间。
但是最后我们遇到了这个错误:
[error] 2019-02-15 09:40:30,466 r.api.Failover2 - [Supervisor-1/Connection-2] Got an error, no more attempts to do. Completing with a failure...
reactivemongo.core.actors.Exceptions$PrimaryUnavailableException: MongoError['No primary node is available! (Supervisor-1/Connection-2)']
Caused by: reactivemongo.core.actors.Exceptions$InternalState: null
at reactivemongo.ConnectAll$IsMaster(400, {{NodeSet Some(PlayappDevelopment-shard-0) Node[playappdevelopment-shard-00-00-tedsb.mongodb.net:27017: Unknown (20/20 available connections), latency=9223372036854775807, authenticated={Authenticated(admin,playapp_rw)}] | Node[playappdevelopment-shard-00-01-tedsb.mongodb.net:27017: Secondary (20/20 available connections), latency=135, authenticated={Authenticated(admin,playapp_rw)}] | Node[playappdevelopment-shard-00-02-tedsb.mongodb.net:27017: Secondary (20/20 available connections), latency=140, authenticated={Authenticated(admin,playapp_rw)}] }})(<time:1550223615628>)
at reactivemongo.IsMaster(400, {{NodeSet Some(PlayappDevelopment-shard-0) Node[playappdevelopment-shard-00-00-tedsb.mongodb.net:27017: Unknown (20/20 available connections), latency=9223372036854775807, authenticated={Authenticated(admin,playapp_rw)}] | Node[playappdevelopment-shard-00-01-tedsb.mongodb.net:27017: …Run Code Online (Sandbox Code Playgroud) 我有一个项目设置playframework 2.2.0和play2-reactivemongo 0.10.0-SNAPSHOT.我想通过他们的id查询一些文档,方式类似于:
def usersCollection = db.collection[JSONCollection]("users")
val ids: List[String] = /* fetched from somewhere else */
val query = ??
val users = usersCollection.find(query).cursor[User].collect[List]()
Run Code Online (Sandbox Code Playgroud)
作为查询我试过:
Json.obj("_id" -> Json.obj("$in" -> ids)) // 1
Json.obj("_id.$oid" -> Json.obj("$in" -> ids)) // 2
Json.obj("_id" -> Json.obj("$oid" -> Json.obj("$in" -> ids))) // 3
Run Code Online (Sandbox Code Playgroud)
第一个和第二个返回空列表,第三个失败,错误assertion 10068 invalid operator: $oid.
我想在MongoDB中更新JSON文档,如下所示:
{
"_id":{"$oid":"52dfc13ec20900c2093155cf"},
"email": "joe@domain.com",
"name": "joe",
"_version": 2
}
Run Code Online (Sandbox Code Playgroud)
...并希望在每次更新时创建这样的vermongo文档:
{
"_id { "_id":{"$oid":"52dfc13ec20900c2093155cf"}, "_version": 1},
"email": "joe@domain.com",
"name": "joe",
"_version": 1,
"_timestamp" : "2014-02-02T00:11:45.542"
}
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的解决方案:
trait MyDao {
...
private val shadowCollection = ReactiveMongoPlugin.db.collection[JSONCollection](
collection.name + ".vermongo"
)
private def toVersioned(deleted: Boolean) = __.json.update(
(__ \ '_id).json.copyFrom((__ \ '_id \ '$oid).json.pickBranch) andThen
(__ \ '_id \ '_version).json.copyFrom((__ \ '_version).json.pick) andThen
// (__ \ '_version).json.put(if (deleted) JsString(s"deleted:$version") else JsNumber(version)) andThen
(__ \ '_timestamp).json.put(Json.toJson(LocalDateTime.now))
)
private def version(doc: JsValue, …Run Code Online (Sandbox Code Playgroud) 在我们的项目中,我们使用的是ReactiveMongo用Play 2.2.1.
问题是,Enumerator[A]返回的形式的数据流ReactiveMongo实际上是一个值对象流,它们没有逗号分隔,并且没有流开始和结束注释,可以视为数组打开和关闭声明.
这会给JSON消费者带来问题JS client,因为预期的格式是
[A1,A2, ...]
所以我们跳入了箍,并通过检查将我们Enumeratee[A]改为Enumerator[String],如果它是第一个元素,或者不是:
var first:Boolean = true
val aToStrs = (as.map(a => {
if(first) {
first = false;
Json.stringify(Json.toJson(a))
} else {
"," + Json.stringify(Json.toJson(a))
}
}))
Ok.chunked(
Enumerator.enumInput(Input.El("[")) andThen
aToStrs andThen
Enumerator.enumInput(Input.El("]")) andThen
Enumerator.enumInput(Input.EOF)
)
Run Code Online (Sandbox Code Playgroud)
这有效,但感觉就像发明轮子一样.
对于这个常见问题,有没有更好的解决方案?
如果我想在诸如 http4s 之类的东西中使用 ReactiveMongo,我必须将 ReactiveMongo 返回的所有 Future 调用包装在 Cats IO 效果中,这样说是否正确?
概括地说,将 ReactiveMongo 合并到 http4s 中需要哪些步骤?