On the go with ReactiveMongo

Hi Guys,

In today’s blog we’ll be discussing about ReactiveMongo. It is a Scala driver that provides fully non-blocking and asynchronous I/O operation. It is designed to avoid any kind of blocking request to your database. Every operation returns immediately, freeing the running thread and resuming execution when it is over. Before starting using reactive mongo, one should have an understanding about mongoDb. So let’s start with an overview of MongoDb.

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. It works on concept of collection and document. Database is a physical container for collection. A Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. It exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. A document is a set of key-value pairs. They have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data. For installation and more information you can visit this MongoDb documentation.

To start with reactiveMongo, you need to add this dependency in your build.sbt file.

libraryDependencies ++= Seq(
  "org.reactivemongo" %% "reactivemongo" % "0.12.3")

Connect to the Database

To connect to the mongodb server you require a Mongo Driver instance and a connection uri which tells the port number on which your mongodb server is running and the database name you want to connect to.

val uri = "mongodb://localhost:port/dbName"
val driver = new MongoDriver 
//To create a connection 

The default port for mongodb server is 27017.

Now, you can create your collections and perform queries. MongoDB offers different kinds of write operations: insertion, update or removal. Using ReactiveMongo, this can be performed asynchronously.

Insert a Document

Insertion is performed using insert function.

def insert(col: BSONCollection, doc: BSONDocument): Future[WriteResult] = {
  val writeRes: Future[WriteResult] = col.insert(doc) writeRes 

WriteResult is a special document that contains information about the write operation, like the number of documents that were updated.

Updating a Document

Update operation is performed using the update() method.

val selector = BSONDocument("name" -> "Jack")

val modifier = BSONDocument(
  "$set" -> BSONDocument(
    "designation" -> "Tester"))

val update = col.update(selector, modifier)

update() operation takes two arguments, first one is the selector which identifies a particular document to be updated and the modifier which specifies the field to be updated.

By default, the update operation only updates a single matching document. You can also indicate that the update should be applied to all the documents that are matching, with the multi parameter.

val update = col.update(selector, modifier, multi = true)

To automatically insert data when there is no matching document use the upsert parameter.

val update = col.update(selector, modifier, upsert = true)

Removing a document

To remove a document one can use the remove() method.

val selector = BSONDocument("firstName" -> "Jack")
val remove = col.remove(selector)

By default, this remove function deletes all the documents that match the selector, but we can change this behaviour by setting the firstMatchOnly parameter to true:

col.remove(selector, firstMatchOnly = true)

Select a document

To display a document, one can use the find() method. This method returns all the documents that match the find criteria.

val selector = BSONDocument("firstName" -> "Jack")

There are many more methods provided by reactive mongo to perform operations on documents.

Here is the link for a sample CRUD application using ReactiveMongo: ReactiveMongoCRUD

References: ReactiveMongo Documentation 

Happy Learning 🙂


This entry was posted in database, knoldus, MongoDB, Non-Blocking, NoSql, sbt, Scala. Bookmark the permalink.

One Response to On the go with ReactiveMongo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s