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())))]
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:
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.