尝试在Play Framework中将ReactiveMongo + BSON重写为JSON时收到错误消息 [英] Get an error message while tring to rewrite ReactiveMongo + BSON to JSON in Play Framework
问题描述
我试图用Json库替换Bson库. 这是有效的原始代码.
I tried to use Json library to replace Bson library. This is the original code which works.
case class City(name: String, population: Int)
object City {
implicit val reader = Macros.reader[City]
}
@Singleton
class CityController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext) extends Controller with MongoController with ReactiveMongoComponents {
def findByMinPopulation(minPop: Int) = Action.async {
import citiesBSON.BatchCommands.AggregationFramework.Match
val futureCitiesList: Future[List[City]] = citiesBSON.aggregate(
Match(BSONDocument("population" -> BSONDocument("$gte" -> minPop)))
).map(_.head[City])
futureCitiesList.map { cities =>
Ok(Json.toJson(cities))
}
}
}
这是使用Json进行编译的代码,但在运行时会出错.
And this is the code using Json which compiles but get an error while running.
case class City(name: String, population: Int)
object City {
implicit val formatter = Json.format[City]
}
@Singleton
class CityController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext) extends Controller with MongoController with ReactiveMongoComponents {
def findByMinPopulation(minPop: Int) = Action.async {
import cities.BatchCommands.AggregationFramework.Match
val futureCitiesList: Future[List[City]] = cities.aggregate(
Match(Json.obj("population" -> Json.obj("$gte" -> minPop)))
).map(_.head[City])
futureCitiesList.map { cities =>
Ok(Json.toJson(cities))
}
}
}
这是我收到的错误消息:
And this is the error message I've got:
[RuntimeException:(,List(ValidationError(List(CommandError [code = 59,errmsg = no such command:'allowDiskUse',bad cmd:'{allowDiskUse:false,explain:false,aggregate:"city",管道:[{$ match:{人口:{$ gte:50000}}}],passwordDocumentValidation:false}',doc:{"ok":0,"errmsg":没有这样的命令:'allowDiskUse',错误的cmd: '{allowDiskUse:否,解释:否,集合:\"city \",管道:[{$ match:{人口:{$ gte:50000}}}]],bypassDocumentValidation:否}',"代码:59 }]),WrappedArray())))
[RuntimeException: (,List(ValidationError(List(CommandError[code=59, errmsg=no such command: 'allowDiskUse', bad cmd: '{ allowDiskUse: false, explain: false, aggregate: "city", pipeline: [ { $match: { population: { $gte: 50000 } } } ], bypassDocumentValidation: false }', doc: {"ok":0,"errmsg":"no such command: 'allowDiskUse', bad cmd: '{ allowDiskUse: false, explain: false, aggregate: \"city\", pipeline: [ { $match: { population: { $gte: 50000 } } } ], bypassDocumentValidation: false }'","code":59}]),WrappedArray())))]
推荐答案
正如@ andrey.ladniy所说,此问题已在版本0.12.0-SNAPSHOT中修复.要使用此版本,请更新build.sbt文件并添加以下内容:
As @andrey.ladniy said, this issue got fixed in version 0.12.0-SNAPSHOT. To use this version, update build.sbt file and add this:
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.12.0-SNAPSHOT"
)
并清除常春藤缓存.要在IntelliJ IDEA中执行此操作,只需选择文件"->使缓存无效/重新启动",然后选择使无效并重新启动".
And clear ivy cache. To do this in IntelliJ IDEA, just select "File" -> "Invalidate Caches / Restart", and select "Invalidate and Restart".
起初我没有清除缓存,即使更新到新版本后也遇到了同样的错误.
I didn't clear cache at first and got the same error even after updated to the new version.
这篇关于尝试在Play Framework中将ReactiveMongo + BSON重写为JSON时收到错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!