Skip to content

Instantly share code, notes, and snippets.

@sgodbillon
sgodbillon / listdatabase.scala
Created April 16, 2014 08:06
List Database command example (ReactiveMongo)
import reactivemongo.core.commands._
import reactivemongo.bson._
import reactivemongo.bson.DefaultBSONHandlers._
import reactivemongo.bson.BSONInteger
import reactivemongo.bson.BSONString
import scala.Some
// { listDatabases: 1 }
/*
listDatabases returns a document for each database
@sgodbillon
sgodbillon / bigdecimalBSON.scala
Last active December 17, 2015 08:59
BigDecimal to BSONDouble Example with ReactiveMongo-BSON 0.9 (two versions: one is precise (the first one), the other is simpler but may lead to wrong value when the value exceeds Double.MaxValue)
import reactivemongo.bson._
// BigDecimal and BigInteger BSON Handlers – the right way :)
object BSONBigDecimalBigInteger {
implicit object BigIntHandler extends BSONDocumentReader[BigInt] with BSONDocumentWriter[BigInt] {
def write(bigInt: BigInt): BSONDocument = BSONDocument(
"signum" -> bigInt.signum,
"value" -> BSONBinary(bigInt.toByteArray, Subtype.UserDefinedSubtype))
def read(doc: BSONDocument): BigInt = BigInt(
@sgodbillon
sgodbillon / UploadGridFS.scala
Created January 21, 2013 23:46
Upload a file into GridFS using Play2.1 and ReactiveMongo 0.8
package controllers
import play.api.mvc._
import play.api.Play.current
import play.modules.reactivemongo._
import customcommands._
import reactivemongo.bson._
import reactivemongo.bson.handlers.DefaultBSONHandlers._
import play.api.libs.json._
import reactivemongo.api.gridfs._
@sgodbillon
sgodbillon / bugStackOverflowErrorPromise.scala
Created December 10, 2012 13:52
Bug Enumerator (StackOverflowError in a Promise)
package bugs
import play.api.libs.iteratee._
import scala.util.Failure
import scala.util.Success
import scala.concurrent.Future
import scala.concurrent.Promise
object StackOverflowErrorBug {
import scala.concurrent.ExecutionContext.Implicits.global
@sgodbillon
sgodbillon / build.sbt
Created November 29, 2012 09:21
ReactiveMongo dependencies
libraryDependencies ++= Seq(
"io.netty" % "netty" % "3.3.1.Final",
"com.typesafe.akka" % "akka-actor_2.10.0-RC2" % "2.1.0-RC2",
"play" % "play-iteratees_2.10" % "2.1-SNAPSHOT",
"ch.qos.logback" % "logback-core" % "1.0.0",
"ch.qos.logback" % "logback-classic" % "1.0.0",
"org.specs2" % "specs2_2.10.0-RC2" % "1.12.2" % "test",
"junit" % "junit" % "4.8" % "test"
)
// finds all documents with lastName = Godbillon and replace lastName with GODBILLON
def findAndModify() = {
val selector = BSONDocument(
"lastName" -> BSONString("Godbillon"))
val modifier = BSONDocument(
"$set" -> BSONDocument("lastName" -> BSONString("GODBILLON")))
val command = FindAndModify(
collection.collectionName,
@sgodbillon
sgodbillon / mongodb.scala
Created May 16, 2012 16:40
plugin example
package play.modules.mongodb
import com.mongodb.casbah.MongoConnection
import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.MongoDB
import play.api._
class MongoPlugin(app :Application) extends Plugin {
lazy val helper :MongoHelper = {
val parsedConf = MongoPlugin.parseConf(app)
@sgodbillon
sgodbillon / mongo.js
Created February 24, 2011 18:09
Minitwitter in full javascript both client/server side with mongodb and nodeJS -- full app available on https://github.com/sgodbillon/minitwitter
// Full source code available on https://github.com/sgodbillon/minitwitter
var nmnLocation = '/path/to/node-mongodb-native'
var Db = require(nmnLocation).Db,
Server = require(nmnLocation).Server;
var host = 'localhost';
var port = 27017;