Playframework 尝试重写ReactiveMongo+;时获取错误消息;在Play框架中从BSON到JSON

Playframework 尝试重写ReactiveMongo+;时获取错误消息;在Play框架中从BSON到JSON,playframework,reactivemongo,play-reactivemongo,Playframework,Reactivemongo,Play Reactivemongo,我尝试使用Json库来替换Bson库。 这是工作的原始代码 case class City(name: String, population: Int) object City { implicit val reader = Macros.reader[City] } @Singleton class CityController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionConte

我尝试使用Json库来替换Bson库。 这是工作的原始代码

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编译的代码,但在运行时出错

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))
    }
  }
}
这是我收到的错误信息:

[RuntimeException:(,List(ValidationError)(List(CommandError[code=59,errmsg=no-this命令:'allowDiskUse',bad cmd:'{allowDiskUse:false,explain:false,aggregate:'city',pipeline:[{$match:{$population:{$gte:50000}}}],bypassDocumentValidation:false}],doc:{“ok”:0,“errmsg:”no-this命令:'allowDiskUse',bad cmd{allowDiskUse:false,explain:false,aggregate:\'city\',管道:[{$match:{population:{$gte:50000}}],绕过documentvalidation:false}',“code:59}]),WrappedArray())]


正如@andrey.ladniy所说,这个问题在版本0.12.0-SNAPSHOT中得到了修复。要使用这个版本,请更新build.sbt文件并添加以下内容:

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.12.0-SNAPSHOT"
)
要在IntelliJ IDEA中执行此操作,只需选择“文件”->“使缓存失效/重新启动”,然后选择“使缓存失效并重新启动”


起初我没有清除缓存,甚至在更新到新版本后也出现了相同的错误。

正如@andrey.ladniy所说,这个问题在版本0.12.0-SNAPSHOT中得到了修复。要使用这个版本,请更新build.sbt文件并添加以下内容:

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.12.0-SNAPSHOT"
)
要在IntelliJ IDEA中执行此操作,只需选择“文件”->“使缓存失效/重新启动”,然后选择“使缓存失效并重新启动”


起初我没有清除缓存,即使在更新到新版本后也出现了相同的错误。

您使用的是什么版本的reactivemongo?@andrey.ladniy版本是0.11.10。有没有办法避免此错误?我的代码没有错误。当我将
放在
匹配
之前时会出现这种错误。我尝试0.12.0。代码工作正常。您使用的是哪个版本的reactivemongo?@andrey.ladniy版本是0.11.10。是否有办法避免此错误?我对您的代码没有错误。当我将
置于
匹配
之前时,会出现这种错误。我尝试0.12.0。代码工作正常。