Get an error message while tring to rewrite ReactiveMongo + BSON to JSON in Play Framework

戏子无情 提交于 2019-12-12 02:53:53

问题


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))
    }
  }
}

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", 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())))]


回答1:


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"
)

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.



来源:https://stackoverflow.com/questions/36184408/get-an-error-message-while-tring-to-rewrite-reactivemongo-bson-to-json-in-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!