你好我有问题我得到回购和控制器女巫返回集合中的所有对象,但我想在收集中搜索"地址"字段我的数据看起来像" adres" "一些adres" booknr" booknr"我想创建具有一些"字符串"的休息返回项目。在字段中这里是我的代码
trait BooksRepo {
def find()(implicit ec: ExecutionContext): Future[List[JsObject]]
def findbyquery()(implicit ec: ExecutionContext): Future[List[JsObject]]
def update(selector: BSONDocument, update: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult]
def remove(document: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult]
def save(document: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult]
}
class BooksMongoRepo(reactiveMongoApi: ReactiveMongoApi) extends PostRepo {
// BSON-JSON conversions
import play.modules.reactivemongo.json._
protected def collection =
reactiveMongoApi.db.collection[JSONCollection]("books")
// val query = Json.obj("adres" -> Json.obj({$regex: /^ABC/i}))
def find()(implicit ec: ExecutionContext): Future[List[JsObject]] =
collection.find(Json.obj()).cursor[JsObject](ReadPreference.Primary).collect[List]()
// def findbyquery()(implicit ec: ExecutionContext): Future[List[JsObject]]= collection.find(query).cursor[JsObject](ReadPreference.Primary).collect[List]()
Controler返回所有书籍
def list = Action.async { implicit request =>
booksRepo.find()
.map(posts => Ok(Json.toJson(posts.reverse)))
.recover { case PrimaryUnavailableException => InternalServerError("Please install MongoDB") }
}