Giter Club home page Giter Club logo

mongo4s's Introduction

#Mongo4s A dsl to interact with MongoDB in scala using the ReactiveMongo library. This project is still in development but any requests are welcome. Feel free to file a bugs/enhancements into issues.

#Building/Sonatype This project is still in active development and doesn't have a stable version (yet). But its up on sonatype! Its cross-published against scala version 2.11.6 and 2.10.5 so it should work with both. The snapshot is not always up to date with whats in the repository. I'm sort of "dogfooding" this.

libraryDependencies += "com.github.kfang" %% "mongo4s" % "0.0.1-SNAPSHOT"

#Why this project exists ##Reason 1 => Verbosity Rather than doing this:

collection.find(BSONDocument("field" -> "value")).options(QueryOpts(skipN = 12)).cursor[BSONDocument].collect[List](upTo = 24)

I'd rather do this:

collection.execute(find("field" -> "value").skip(12).limit(24).asList)

or if you don't like the dots:

collection.execute(find sel "field" -> "value" skip 12 limit 24 asList)

##Reason 2 => CRUD I'm okay with the IFUR(insert/find/update/remove) paradigm but not everyone is familiar with it and I don't think it has a wikipedia page. But a lot people know CRUD(create/read/update/delete). The dsl provides the verbage to interface the two. For example, these two lines are the same:

find("field" -> "value")
read("field" -> "value")

or

insert(BSONDocument("_id" -> 1))
create(BSONDocument("_id" -> 1))

Better documentation will exist once more of this stuff is implemented but the basics are in the FindDsl.scala

#TODO

  • Find DSL
  • Insert DSL
  • Delete DSL
  • Update DSL
  • Count DSL
  • Distinct DSL
  • '$' shortcuts (err.. 1/2? complete)

#Usage ##MongoConnPool The MongoConnPool contains your MongoConnection. For every set of nodes you need to attach to, you have one MongoConnection. This one MongoConnPool is then shared by your MongoDatabases.

val system = ActorSystem()
val nodes = Seq("localhost")
val connPool = MongoConnPool(system, nodes)

##Connecting to a Database

case class SampleModel(_id: String, data: String)
object SampleModel { implicit val handler = Macros.handler[SampleModel] }

case class MyMongoDB(conn: MongoConnPool) extends MongoDatabase {
    val name: String = "db_name"
    val MyCollection: MongoCollection = getCollection[SampleModel]("my-collection")
}

##Using the DSL

import com.github.kfang.mongo4s.MongoDsl._

//connect to the database using the connection pool
val Database = MyMongoDB("my-db-name", connPool)

//execute a query on a collection
Database.MyCollection.execute(count("data" -> "data-value")).map(c => {
  println("There are " + c + " document that match {'data':'data-value'}")
})

//find/read out documents
val getOne: Future[Option[SampleModel]] = Database.MyCollection.execute(find.id("id-to-find").one)
val getOne: Future[Option[SampleModel]] = Database.MyCollection.execute(find.sel("_id" -> "id-to-find").one)
val getList: Future[List[SampleModel]] = Database.MyCollection.execute(find.sel().limit(20).asList)

//insert/create a document
val model = SampleModel("some-id", "my-data")
Database.MyCollection.execute(insert.model(model))
Database.MyCollection.execute(insert.doc(BSONDocument("_id" -> "some-id", "data" -> "my-data")))

//remove/delete a document
val model = SampleModel("some-id", "my-data")
Database.MyCollection.execute(remove.model(model))
Database.MyCollection.execute(remove.id(model.id))
Database.MyCollection.execute(remove.sel("_id" -> model.id))

//TODO: update a document
//TODO: distinct values

##Shorcuts!! I find the whole BSONDocument("field" -> BSONDocument("field" -> BSONDocument("field" -> "value"))) thing a little long to type out. So... here you go!

import com.github.kfang.mongo4s.MongoShortcuts._
//you could do this...
BSONDocument("field" -> BSONDocument("field" -> BSONDocument("field" -> "value")))
//or this!
Bdoc("field" -> BDoc("field" -> BDoc("field" -> "value")))

//$inc
BSONDocument("$inc" -> BSONDocument("field" -> 1))
Bdoc($inc("field" -> 1))

//$currentDate
BSONDocument("$currentDate" -> BSONDocument("field" -> BSONDocument("$type" -> "date")))
Bdoc($currentDate("field"))

mongo4s's People

Contributors

kfang avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.