本文整理汇总了Scala中play.modules.reactivemongo.json.collection.JSONCollection类的典型用法代码示例。如果您正苦于以下问题:Scala JSONCollection类的具体用法?Scala JSONCollection怎么用?Scala JSONCollection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JSONCollection类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: find
//设置package包名称以及导入依赖的类
package repos
import scala.concurrent.{ExecutionContext, Future}
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import play.api.libs.json.{JsObject, Json}
import reactivemongo.api.commands.WriteResult
import play.modules.reactivemongo.ReactiveMongoApi
import javax.inject._
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.ReadPreference
trait PostRepo {
def find() (implicit ec: ExecutionContext): Future[List[JsObject]]
def select(selector: BSONDocument) (implicit ec: ExecutionContext): Future[Option[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 PostRepoImpl @Inject() (reactiveMongoApi: ReactiveMongoApi) extends PostRepo {
def collection = reactiveMongoApi.db.collection[JSONCollection]("posts");
override def find() (implicit ec: ExecutionContext): Future[List[JsObject]] = {
val genericQueryBuilder = collection.find(Json.obj());
val cursor = genericQueryBuilder.cursor[JsObject](ReadPreference.Primary);
cursor.collect[List]()
}
def select(selector: BSONDocument)(implicit ec: ExecutionContext): Future[Option[JsObject]] = {
collection.find(selector).one[JsObject]
}
def update(selector: BSONDocument, update: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult] = {
collection.update(selector,update)
}
def remove(document: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult] = {
collection.remove(document)
}
def save(document: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult] = {
collection.update(BSONDocument("_id" -> document.get("_id").getOrElse(BSONObjectID.generate)), document, upsert = true)
}
}
示例2: BalanceInfoController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Singleton}
import models.BalanceInfo
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.api.{Cursor, ReadPreference}
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class BalanceInfoController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext)
extends Controller with MongoController with ReactiveMongoComponents {
val balances: JSONCollection = db.collection[JSONCollection]("balance_info")
def get(name: String) = Action.async {
val balanceCursor: Cursor[BalanceInfo] =
balances.find(Json.obj("name" -> name)).cursor[BalanceInfo](ReadPreference.primary)
val balanceFuture: Future[List[BalanceInfo]] = balanceCursor.collect[List]()
balanceFuture.map { data => Ok(Json.toJson(data)) }
}
}
示例3: ValorationDAO
//设置package包名称以及导入依赖的类
package dao.impl
import play.api.libs.json.{ JsObject, Json }
import reactivemongo.api.ReadPreference
import reactivemongo.api.commands.WriteResult
import reactivemongo.bson.{ BSONObjectID, BSONDocument }
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.bson.{ BSONObjectID, BSONDocument }
import scala.concurrent.{ ExecutionContext, Future }
import dao.TValorationDAO
import play.modules.reactivemongo.json.collection.JSONCollection
import play.modules.reactivemongo.json.BSONDocumentWrites
import play.modules.reactivemongo.json.JsObjectDocumentWriter
import reactivemongo.bson.Producer.nameValue2Producer
import scala.annotation.implicitNotFound
class ValorationDAO(reactiveMongoApi: ReactiveMongoApi) extends TValorationDAO {
// BSON-JSON conversions
import play.modules.reactivemongo.json._
protected def collection =
reactiveMongoApi.db.collection[JSONCollection]("valorations")
def uuid = java.util.UUID.randomUUID.toString
def find()(implicit ec: ExecutionContext): Future[List[JsObject]] =
collection.find(Json.obj()).cursor[JsObject](ReadPreference.Primary).collect[List]()
def findBySpot(spotId: String)(implicit ec: ExecutionContext): Future[List[JsObject]] =
collection.find(BSONDocument("spot_id" -> spotId)).cursor[JsObject]().collect[List]()
def findById(genId: String)(implicit ec: ExecutionContext): Future[Option[JsObject]] =
collection.find(BSONDocument("_id" -> genId)).one[JsObject]
def findById2(genId: String)(implicit ec: ExecutionContext): Future[Option[BSONDocument]] =
collection.find(BSONDocument("_id" -> genId)).one[BSONDocument]
def save(document: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult] =
collection.update(BSONDocument("_id" -> document.get("_id").getOrElse(BSONObjectID.generate)), document, upsert = true)
}
示例4: find
//设置package包名称以及导入依赖的类
package repos
import javax.inject.Inject
import play.api.libs.json.{JsObject, Json}
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.ReadPreference
import reactivemongo.api.commands.WriteResult
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import scala.concurrent.{ExecutionContext, Future}
trait ApiRepo {
def find()(implicit ec: ExecutionContext): Future[List[JsObject]]
def save(selector: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult]
}
class MongoApiRepo @Inject()(reactiveMongoApi: ReactiveMongoApi) extends ApiRepo {
def collection = reactiveMongoApi.db.collection[JSONCollection]("users")
override def find()(implicit ec: ExecutionContext): Future[List[JsObject]] = {
val genericQueryBuilder = collection.find(Json.obj())
val cursor = genericQueryBuilder.cursor[JsObject](ReadPreference.Primary)
cursor.collect[List]()
}
override def save(document: BSONDocument)(implicit ec: ExecutionContext): Future[WriteResult] = {
collection.update(BSONDocument("_id" -> document.get("_id").getOrElse(BSONObjectID.generate)), document, upsert = true)
}
}
示例5: ctx
//设置package包名称以及导入依赖的类
package base
import play.modules.reactivemongo.json.JSONSerializationPack
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.DB
import scala.concurrent.{Future, ExecutionContext}
def ctx(db: DB, collectionName: Symbol): JSONCollection = ctx(db, collectionName.name)
private def ctx(db: DB, collectionName: String): JSONCollection =
db.collection[JSONCollection](collectionName)
def LastOk = reactivemongo.core.commands.LastError(true, None, None, None, None, 0, false)
def LastError(error: String) = reactivemongo.core.commands.LastError(false, None, None, Some(error), None, 0, false)
def bulkInsert[T]
(ctx: JSONCollection, lst: Seq[T])
(implicit writer: JSONSerializationPack.Writer[T], ec: ExecutionContext): Future[Int] = {
lst.foldLeft(Future.successful(0)) { (acc: Future[Int], x: T) =>
for {
n <- acc
error <- ctx.insert(x)
i = if (error.ok) 1 else 0
} yield n + 1
}
}
}
示例6: ctx
//设置package包名称以及导入依赖的类
package biz.interop
import play.api.libs.json.{JsBoolean, JsObject, JsString, Json}
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.DB
import scala.concurrent.{ExecutionContext, Future}
trait CanConnectDB {
def ctx(db: DB): JSONCollection
val defaultIdentityField = "_id"
def identityQuery(value: String, identityField: String = defaultIdentityField): JsObject =
Json.obj(identityField -> JsString(value))
def fieldsProjection(fields: String*): JsObject = {
val project = fields.filter(! _.trim.isEmpty).map(_ -> JsBoolean(true))
JsObject(project)
}
val allQuery = Json.obj()
def count
(db: DB, value: String, identityField: String = defaultIdentityField)
(implicit ec: ExecutionContext): Future[Int] = {
ctx(db).count(Some(identityQuery(value, identityField)))
}
def any
(db: DB, value: String, identityField: String = defaultIdentityField)
(implicit ec: ExecutionContext): Future[Boolean] = {
count(db, value, identityField).map(_ > 0)
}
}