Simple Medical Record System with MongoDB

Introduction

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.

Concepts

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

Installation of MongoDB requires few steps.

  1. $ sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
  2. $ echo “deb http://repo.mongodb.org/apt/ubuntu 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

http://central.maven.org/maven2/org/mongodb/mongo-java-driver/ (Need to select the version which matches your MongoDB server)

Sample

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.net.UnknownHostException;
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();
 app.createConnection();
 app.insertRecords();

 app.findRecords("flu");

 }

 /**
 * 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");

 col.insert(doc1);
 col.insert(doc2);
 col.insert(doc3);
 col.insert(doc4);
 col.insert(doc5);

 }

 /**
 * 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: " + dBCursor.next());

 }
 }

}

Result

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"}

References

[1] Official website – https://www.mongodb.org/

[2] Tutorialspoint MongoDB – http://www.tutorialspoint.com/mongodb/index.htm

Advertisements

One thought on “Simple Medical Record System with MongoDB

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