My Experiences with Scala – MongoDB

I would like to share my own experiences on – ‘Scala’ and ‘Mongo DB’.   ‘Scala’ as a language was introduced in the market in 2004 and ‘Mongo DB’ in 2009.


Scala was developed by Martin Odersky in 2004 on the Java and .NET platform. It is a multi-paradigm programming language designed as a “better Java” – building on top of the Java virtual machine (JVM) and maintaining strong interoperability with Java, while at the same time integrating functional programming along with Java’s object-oriented programming model.

Like Java, Scala is statically typed and strongly object-oriented, compiles code into Java bytecode, allowing Scala code to be run on the JVM and permitting Java libraries to be freely called from Scala.

Features :-

Scala adds a large number of features as compared with Java, and has some fundamental differences in its underlying model of expressions and types.

Syntactic Flexibility

As mentioned above, Scala has a good deal of syntactic flexibility, compared with Java. The following are some examples:

  1. Semicolons are unnecessary; lines are automatically joined if they begin or end with a token that cannot normally come in this position, or if there are unclosed parentheses or brackets.
  2. The final () can be left off of a function that takes no arguments.

Unified type system

Java makes a sharp distinction between primitive types (e.g. int and boolean) and reference types (any class). Only reference types are part of the inheritance scheme. In Scala, however, all types inherit from a top-level class Any, whose immediate children are AnyVal (value types, such as Int and Boolean) and AnyRef (reference types, as in Java). In Scala boxing and unboxing is completely transparent to the user.


Instead of the Java “for…each” loop for looping through an iterator, Scala has a much more powerful concept of for-comprehensions. For-comprehensions allow a new collection to be generated by iterating over an existing collection, and make the type of the new collection correspond to that of the existing collection whenever possible.

Why Scala?

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.

  • Scala is object-oriented
  • Scala is functional too
  • Scala is statically typed:
  • Scala interoperates with Java and .NET

When to choose Scala?

Even though Scala is most flexible as compare to Java, we should not use this language for developing applications which are   transaction oriented involved in just CRUD (create, read, update, delete).  It would be more appropriate to use Scala for developing applications which involve features like Risk Management, Data Collection, Market Simulation and Trading/betting exchange markets, Social networking and rich Graphical Games.

What differentiates Scala from others:-

  • Scala is ideal for today’s scalable, distributed, component-based applications that support concurrency and distribution.
  • Scala is statically typed that means that the type of some variable is immutable during the whole execution of the program.
  • There is modular mixin-composition for classes – some hack to enable multiple inheritance in Scala, which solves the diamond problem through linearity of the inheritance hierarchy via traits.
  • Support of so functions may have other functions as arguments which enables using anonymous functions.
  • Lower risk to use Scala in an existing Java Application because Scala works seamless with existing Java Code.
  • High level type system with variance annotations, compound types, lower and upper bounds for types
  • Usage of inner and anonymous classes
  • Implicit conversions – that means a function take one type as an argument and returns to another type (like converting an Integer into String)

Mongo DB:-                                              

In 2009 MongoDB was released in market as a stand-alone product with an AGPL license. MongoDB (from “humongous”) is an open source document-oriented database system developed and supported by 10gen. It is part of the NoSQL family of database systems. Instead of storing data in tables as is done in a “classical” relational database, MongoDB stores structured data as JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.

Features of MongoDB :-

  • Ad hoc queries MongoDB supports search by field, range queries, regular expression searches. Queries can return specific fields of documents and also include user-defined JavaScript functions.
  • Indexing Any field in a MongoDB document can be indexed.

Replication MongoDB supports master-slave replication. A master can perform reads and writes. A slave copies data from the master and can only be used for reads or backup

  • Load balancing MongoDB scales horizontally using sharding. MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure
  • File storage MongoDB could be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files.
  • Aggregation MapReduce can be used for batch processing of data and aggregation operations. The aggregation framework enables users to obtain the kind of results for which the SQL GROUP BY clause is used.
  • Server-side JavaScript execution JavaScript can be used in queries, aggregation functions, are sent directly to the database to be executed.
  • Capped collections MongoDB supports fixed-size collections called capped collections. This type of collection maintains insertion order and, once the specified size has been reached, behaves like a circular queue.


When to choose Mongo DB?

If your DB is 3NF and you don’t do any joins (you’re just selecting a bunch of tables and putting all the objects together, like what most people do in a web app), MongoDB would probably be the best for you. If your project contains much more use of caching, storage of user sessions, maintaining a watchdog log, queue management, etc.       this is surely the ideal DB to be played with. Therefore, it is much suitable for Projects like CRM, Search Engines, Social Networking, Online trading, web portals having more graphical usages(like 3D PLM, Catia, product design) etc. This DB is most suitable for News & Media and online marketing sector. Mongo DB is not suitable when there are multiple and huge number of statistical calculations, therefore the projects on Banking, Share Market, Scientific Researches (like Quantum Mathematics, Astronomical expeditions) are certainly not the suitable.

This was a brief description on Scala and Mongo DB. Now, I want to share my own experiences related to this. When I had started to use the Mongo DB database, I was very surprised at that time because till that time I was generally used to or familiar with the very common databases like oracle, MySql, MS Access and SQL 2005 etc., which are the classical ‘Relational Database’. First time I am using the Non-Relational database which is ‘Document Oriented Database’. In this database some new things are introduced which I went through at first time and because of these things I had been really excited to use Mongo DB database.

Alright, let me give a chance to elaborate more on technical aspects. When we use the relational database, we have to create a new database first and it’s schema as well. After that we create the tables and decide that which fields we do need in those tables. After all these steps are done manually then only it is ready to use. However, in Mongo DB, when you type like ‘use Test’ command then the ‘Test’ database is created dynamically and it immediately becomes ready to use. There is no need to define the schema. Also, when you fire the insert query into the database then Mongo DB creates table automatically and also it creates the columns automatically according to your query.   So, it reduces   Database related exceptions. I desperately want to share lots of stuff like this with you my friends, but due to limitations I can only brief about Mongo DB.

Everyone likes to twit on day to day activities (I am also not the exception). But, do you know that, has also been developed using Scala language. Yes, Now   you are quite curious about Scala. Scala is the only language which provides the functionality of Object Oriented and Functional Oriented. If you are writing a code snippet in Java for 10-15 lines, in Scala the efforts get reduced to 3-5 lines. Initially, you will stuck on many things including syntax, logic implementation, DB management and very new set of errors. However, when you will spend your time on Scala, you will eventually be friendly with this stuff and then I am sure, you will start liking Scala.

Up course, my way to learn these things are equally hard enough just like to learn human languages like Japanese or Chinese. Initially, I struggled like anything even for minor things like syntax, DB management, multiple language interoperability etc. Even though this struggle, I always prefer for the Scala and Mongo DB, because when I started playing with them practically I came to know lots of their benefits considering time and functionality. If you want to switch from your technology say from Java, to Scala or Mongo DB it would be quite difficult to approach this, because we have a tendency to think in our well known platform functionality and here in Scala & MongoDB the things are little different even including syntax. But, I am sure, that you will also be pleased like me, to use this for long term enhancements in your respective projects because of Scala’s time saving and effort saving attitude.

If I have already excited you on these things then these things will be clearer to you when you start working on these technologies, and one day you will also tend to write benefits and experiences of Scala and MongoDB like me.

Interested in our Development Services?

Please enable JavaScript in your browser to complete this form.
By submitting this form, you agree that you have read and understand Apexon’s Terms and Conditions. You can opt-out of communications at any time. We respect your privacy.

Other stories you may enjoy...

The Internet: Then and Now

It has been a big week in tech. Not only did we have the Google I/O keynote yesterday (last week’s blog covered that in more detail), but also Kleiner Perkins Caufield &...

Is Your Healthcare App Safe Enough? Are You Sure?

Seemingly, every company from sporting goods manufacturers to medical insurance providers to smartphone makers are getting into the heathcare app game.   Healthcare apps are...

Apexon Powered Mobile Testing

Mobility is the ‘new normal’ now. It has been largely adopted by enterprises across all domains - banks, hospitals, hotels, retail and travel chains. Enterprises have matured...