Scala : Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. More…
Play Framework : Play 2.0 is a high-productivity Scala web application framework that integrates the components and APIs you need for modern web application development.
Play is based on a lightweight, stateless, web-friendly architecture. More…
MongoDB : MongoDB is a scalable, high-performance, open source NoSQL database. More…
Here we’ll have a look to use all these three together with help of small code snippets.
Creating a Play Scala Project using new command like :
Adding Dependecies :
Add the following dependencies in ‘Build.scala’ File in order to use MongoDB (Here we’ll be using SalatDAO).
Preparing Requests (GET and POST) : Here we are taking an example of a simple GET request.
You can define the requests path and the correspondent methods in “routes” file like :
Here we are defining that a GET request on this URL ” /callMe” would hit the method testMethodForPlayTutorail in Tutorial Controller.
Writing Controller : Now we are going to write Tutorial controller under “app/controllers”.
Now you can run your application using following command & you’ll find your output at http://localhost:9000/callMe
Connecting to MongoDB :
You’d require mongoDB running locally. We’ll use SalatDAO framwork to store the Data in mongoDB.You’d get an idea from this example of how to interact with MongoDB.
Firstly let us create the MongoDB connection class
Saving data in MongoDB :
Now recall & modify the testMethodForPlayTutorial method.
Now on hitting the URL http://localhost:9000/callMe would generate the GET request that will create a database test_database and stores the data in collection saved_data.
GitHub Repo for this example.
6 thoughts on “Working with Play framework using Scala and MongoDB2 min read”
Leon Radley has created a plugin for using salat with play2  for an example on how this is used see .
I am working on doing the computer-database-mongo example in java aswell and will send a pull request to Play when it is done. So there will be a mongo sample in Play this fall hopefully.
Any tutorials on how to use mongodb with playframework 2 and Java?
Hi Nicowernli ,
We used to work on Scala here and we are using MongoDB with play 2.0 & Scala.
Might be this link will help you for starting with Java and MongoDB :
Thank you very much Neelkanth your blog realy help me. I find all thing which i am searching from the morning . Thank you very much keep on posting more blog on mongo and play.
It is a very good, simplest example ever.. Cheers
Comments are closed.