在使用新的 builders 来处理delete.many
和update.many
时遇到了一些困难,同时尝试将以前版本的(有效)代码转换为reactmongo 0.16.5({ {1}},您将看到;我在Play插件中使用了它,因此处理的是JSON(而不是BSON)
我要去the official documentation here。我的错误在"org.reactivemongo" %% "play2-reactivemongo" % "0.16.5-play26", "org.reactivemongo" %% "reactivemongo-akkastream" % "0.16.5"
和update
上都是相似的,因此我只在此处发布delete
来保持修饰。
更新命令
update
自定义更新生成器模型
def updateMany(collName: String)(quJsa: JsArray)(orderedBool: Boolean = false): Future[MultiBulkWriteResult] = {
lazy val updateBuilder = getCollection(collName).map(_.update(orderedBool))
quJsa.asOpt[Seq[JsObject]].map(
_.map(
x => x.as[MongoUpdateBuilder]
)
).map(
_.map(
x => updateBuilder.flatMap(
_.element(q = x.q, u = x.getSetUpdate, upsert = x.upsertBool, multi = x.multiBool)
)
)
).map(
x => Future.sequence(x)
).map(
_.flatMap(
x => updateBuilder.flatMap(
_.many(x)
)
)
).getOrElse(getMultiBulkWriteResultErrorF("input not recognised as jsarr"))
}
错误容器
case class MongoUpdateBuilder(q: JsObject, u: JsObject, upsertBool: Boolean, multiBool: Boolean) {
def getSetUpdate = Json.obj("$set" -> u)
}
object MongoUpdateBuilder {
implicit val mongoUpdateBuilderFormat = Json.format[MongoUpdateBuilder]
}
主要问题:
flatMap方法没有类型参数:(f:reactmongo.play.json.collection .JSONCollection#UpdateBuilder => scala.concurrent.Future [S])(隐式执行者:scala.concurrent.ExecutionContext)scala.concurrent.Future [S]存在,因此可以将其应用于参数(reactivemongo.play.json.collection .JSONCollection#UpdateBuilder => scala.concurrent.Future [_1.UpdateCommand.UpdateElement] forSome {val _1:reactmongo.play.json.collection.J SONCollection})[错误] ---因为- [错误]参数表达式的类型与形式参数类型不兼容; 发现[错误]:reactivemongo.play.json.collection.JSONCollection#UpdateBuilder => scala.concurrent.Future [_1.UpdateCommand.UpdateElement] forSome {val _1:reactmongo.play.jso n.collection.JSONCollection} [错误]必需:reactmongo.play.json.collection.JSONCollection#UpdateBuilder => scala.concurrent.Future [?S] [错误] x => updateBuilder.flatMap(
所以问题似乎出在这一行- def getMultiBulkWriteResultErrorF(errStr: String): Future[MultiBulkWriteResult] = {
val mbwr = MultiBulkWriteResult(
ok = false,
n = 0,
nModified = 0,
upserted = Seq(),
writeErrors = Seq(WriteError(index = 0, code = 404, errmsg = errStr)),
writeConcernError = None,
code = Some(404),
errmsg = Some(errStr),
totalN = 0
)
Future.successful(mbwr)
}
。这些类型(updateBuilder.flatMap
和Future
的{{1}}不能是flattened
。所以我正在努力与此。如果您可以在此处看到问题,请与我们联系。非常感谢!