• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala JSONCollection类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)
  }
  
} 
开发者ID:Rheriel,项目名称:LightHouse,代码行数:52,代码来源:PostRepo.scala


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

} 
开发者ID:qza,项目名称:balance-service-play,代码行数:30,代码来源:BalanceInfoController.scala


示例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)

} 
开发者ID:ElComite,项目名称:placesmapper_back,代码行数:42,代码来源:ValorationDAO.scala


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

} 
开发者ID:Jagordon,项目名称:play-api,代码行数:36,代码来源:api.scala


示例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
    }
  }
} 
开发者ID:scozv,项目名称:bolero,代码行数:30,代码来源:mongo.scala


示例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)
  }
} 
开发者ID:scozv,项目名称:bolero,代码行数:33,代码来源:CanConnectDB.scala



注:本文中的play.modules.reactivemongo.json.collection.JSONCollection类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Ignore类代码示例发布时间:2022-05-23
下一篇:
Scala UserDAO类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap