Month: December 2015

Simple Medical Record System with MongoDB


I have worked with SQL for some years, so this application can be done with MySQL or any-other RDBMS system. But NoSQL(Not Only SQL) is getting popular these days. Unlike RDBMS, NoSQL has number of pros and cons when compared. Basically there are few major categories of NoSQL databases exists. Key-value stores, document database, wide-column stores, graph databases are  few of those categories.


This is my hands-on application using MongoDB. MongoDB falls into document database category. These databases are designed to store documents which are encoded. The column values are semi-structured, so every row no-need to have exact number of attributes. Also records are searchable using both keys and values.

Before understanding code, first you need to MongoDB terminology with compared to RDBMS

RDBMS —————– MongoDB
Database —————– Database
Table ——————— Collection
Tuple/Row ————– Document
Column —————— Field
Table Join ————— Embedded Documents


Installation of MongoDB requires few steps.

  1. $ sudo apt-key adv –keyserver hkp:// –recv 7F0CEB10
  2. $ echo “deb trusty/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
  3. $ sudo apt-get update
  4. $ sudo apt-get install -y mongodb-org

You also need to have MongoDB JDBC driver to be at Java classpath to execute the code (Need to select the version which matches your MongoDB server)


This sample application creates a collection for records and insert set of medical records. Then it query on a given decease. So that administrator can get an idea on how many people had the same decease.

package medicalrecordsapp;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import java.util.logging.Level;
import java.util.logging.Logger;

 * @author buddhima
public class MedicalRecordsApp {

 DBCollection col = null;

 * Main executor
 * @param args the command line arguments
 public static void main(String[] args) {
 MedicalRecordsApp app = new MedicalRecordsApp();



 * Establish connection
 public void createConnection() {
 try {
 MongoClient mongoClient = new MongoClient("localhost", 27017);
 DB db = mongoClient.getDB("firstdb");
 System.out.println("Connect to database successfully");

 if (!db.collectionExists("records")) {
 col = db.createCollection("records", new BasicDBObject());

 // Fetching the Collection
 col = db.getCollection("records");

 } catch (UnknownHostException ex) {
 Logger.getLogger(MedicalRecordsApp.class.getName()).log(Level.SEVERE, null, ex);


 * Insert all records
 public void insertRecords() {
 BasicDBObject doc1 = new BasicDBObject("name", "Sam").
 append("age", 20).
 append("disease", "chicken pox");

 BasicDBObject doc2 = new BasicDBObject("name", "Jack").
 append("age", 22).
 append("disease", "flu");

 BasicDBObject doc3 = new BasicDBObject("name", "Tom").
 append("age", 10).
 append("disease", "dengi");

 BasicDBObject doc4 = new BasicDBObject("name", "Robert").
 append("age", 30).
 append("disease", "flu");

 BasicDBObject doc5 = new BasicDBObject("name", "Richard").
 append("age", 45).
 append("disease", "flu");



 * Fetch all relevant records
 * @param disease Name of the disease
 public void findRecords(String disease) {

 BasicDBObject query = new BasicDBObject("disease", disease);

 // Get records, omitting _id field
 DBCursor dBCursor = col.find(query, new BasicDBObject("_id", 0));

 while (dBCursor.hasNext()) {

 System.out.println("Record: " +;




Connect to database successfully
Record: { "name" : "Jack" , "age" : 22 , "disease" : "flu"}
Record: { "name" : "Robert" , "age" : 30 , "disease" : "flu"}
Record: { "name" : "Richard" , "age" : 45 , "disease" : "flu"}


[1] Official website –

[2] Tutorialspoint MongoDB –