在Play框架中将ReactiveMongo + BSON重写为JSON时获取错误消息

时间:2016-03-23 17:03:24

标签: 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: 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 such command:'allowDiskUse',bad cmd:'{allowDiskUse:false,explain:false,aggregate:“city”,pipeline :[{$ match:{population:{$ gte:50000}}}],bypassDocumentValidation:false}',doc:{“ok”:0,“errmsg”:“没有这样的命令:'allowDiskUse',bad cmd: '{allowDiskUse:false,explain:false,aggregate:\“city \”,pipeline:[{$ match:{population:{$ gte:50000}}}],bypassDocumentValidation:false}'“,”code“:59 }]),WrappedArray())))]

1 个答案:

答案 0 :(得分:2)

正如@ 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中执行此操作,只需选择“文件” - > “使高速缓存/重新启动无效”,并选择“无效并重新启动”。

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