Saturday 18 October 2014

How Java Developers can get started MongoDB?

About MongoDB (from wikipedia) - In case you don't know what it is, then MongoDB (from "humongous") is a free and open-source cross-platform document-oriented database software. Classified as a NoSQL database, MongoDB avoids the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.  It provides high performance, high availability, and easy scalability.

From java developers outlook - If MongoDB is deliberated to persist some or all data in java based application architecture, then java developers need to write java code in data access layer of application to perform CRUD and other operations with MongoDB backend using Java MongoDB Driver API or may be Spring Data MongoDB (just imagine this something like writing JDBC code to exchange data between JavaEE app and RDBMS). --- On a separate note, are you aware about the term polyglot persistence describing hybrid approach to persistence by using different data storage technologies to handle varying data persistence needs!

M101J Course: MongoDB for Java Developers

Speaking of MongoDB, recently I got opportunity to attend "M101J: MONGODB FOR JAVA DEVELOPERS" course which is a free 7 weeks online course and expects attendees to spend 6-8 hours per week as per the designed course agenda. The course delivers everything java developers need to know for getting started with building a MongoDB-based java application, which includes basic MongoDB installation, JSON, schema design, crud operations, indexing, aggregation framework, application engineering  and working with MongoDB java language drivers. The data interaction between Java app and MongoDB is practically demonstrated by building a java based blogging platform, backed by MongoDB. Moreover, one will receive a certificate of M101J course completion at the end, if achieves a 65% or above on graded material.

As a matter of fact, the quality of M101J is elegant and specially designed for java developers to get started with MongoDB. If you wish to learn MongoDB or don't have exposure of any of NoSQL database, then I highly endorse to register and attend this course.

Self-paced Getting Started with MongoDB

In case you can't manage to accomplish the M101J course, then you can get started with MongoDB as below.

Firstly I suggest to go through MongoDB@TutorialsPoint course to get fair understanding of MongoDB. Don't just read, but also try practically as you progress.

Once you become familiar with MongoDB, then I suggest to refer the sample java code snippet used in the M101J course. For this, you can setup M101J java project and explore code as below.

  1. Download / Clone "MongoDB-ForJavaApp/M101J" project to your local machine from GitHub. 
  1. Import "M101J" project in Eclipse. Fix build path errors, such as JRE configuration. This is maven based project, so it would download dependent jar files from internet.
  1. Ensure MongoD process is running.
  2. Go to "com.tengen.helloworld" package in "src/main/java" folder of the project. As per "README.md" file, create "hello" collection and insert data. Now see code of "HelloWorldMongoDBStyle.java" and run it; which should connect to "course" db in MongoDB, get data from "course" collection and finally print on console. The other hello world programs in this folder are to demo basic usage of freemarker (templating engine) and spark (micro web framework which allows to setup routes for mapping urls and having embedded Jetty) frameworks, which are used to build frontend templates and controller components of blogging app tutorial. For example - just run "HelloWorldMongoDBSparkFreemarkerStyle.java" program and access "http://localhost:8082" url in the browser, so it would fetch data from MongoDB and show on browser page.
  3. Go to "com.tengen.crud" package in "src/main/java" folder of the project to explore java code for Mongo crud operations.
  4. Finally explore sample blogging application.
    • How to run it?
      • Ensure MongoDB instance "mongodb://localhost" is up and running, otherwise you may need to change default configuration is in BlogController.java.
      • Run BlogController.java as java application or using maven command. This would run it on HTTP 8082 port using Spark framework.
      • Now you should be able to access it in browser using this URL "http://localhost:8082/" and can play with the blogging application functionality.

Also Refer