Monday, February 15, 2016

Mongodb-Jongo-JAVA Wrapper


What is MongoDB(https://www.mongodb.org/)

MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.

Document Database

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.
{
   "_id" : ObjectId("54c955492b7c8eb21818bd09"),
   "address" : {
      "street" : "2 Avenue",
      "zipcode" : "10075",
      "building" : "1480",
      "coord" : [ -73.9557413, 40.7720266 ],
   },
   "borough" : "Manhattan",
   "cuisine" : "Italian",
   "grades" : [
      {
         "date" : ISODate("2014-10-01T00:00:00Z"),
         "grade" : "A",
         "score" : 11
      },
      {
         "date" : ISODate("2014-01-16T00:00:00Z"),
         "grade" : "B",
         "score" : 17
      }
   ],
   "name" : "Vella",
   "restaurant_id" : "41704620"
}
The advantages of using documents are:
  • Documents (i.e. objects) correspond to native data types in many programming languages.
  • Embedded documents and arrays reduce need for expensive joins.
  • Dynamic schema supports fluent polymorphism.

Key Features

High Performance

MongoDB provides high performance data persistence. In particular,
  • Support for embedded data models reduces I/O activity on database system.
  • Indexes support faster queries and can include keys from embedded documents and arrays.

High Availability

To provide high availability, MongoDB’s replication facility, called replica sets, provide:
  • automatic failover.
  • data redundancy.
A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.

Automatic Scaling

MongoDB provides horizontal scalability as part of its core functionality.
  • Automatic sharding distributes data across a cluster of machines.
  • Replica sets can provide eventually-consistent reads for low-latency high throughput deployments.

what I have done ? ?

I have build a wrapper so you can this database in less coding.I used another implementation called Jongo (http://jongo.org/) which is also build to use MongoDB .Using this wrapper you can save ,delete,update or find java objects in MongoDb.I have implemented UpdateQuerey which makes quering less messy in your programme.
In this you don't have to worry about making connection with database and only thing you have to do is provide what is your primary key in your class using "@MongoId" annotaion .You can find more details on this page : http://jongo.org/


Query results are automatically mapped to objects. By default, this relies on Jackson; it respects document structure, handles lists and ignores missing attributes. It just need a no-arg constructor, even private (if the object has to be immutable, @JsonCreator annotation can be use instead).
                                 _id is a unique identifier available on every Mongo document. If it isn't setted, it is generated. To handle it with Jongo, one attribute has to be named _id or annotated with @MongoId (alias for @JsonProperty("_id")). It can be handled with the dedicated ObjectId class or with a simple String — annotated @MongoObjectId.

Examples : 
public class Friend { // manual private long _id; } public class Friend { // manual @MongoId private long key; } public class Friend { // manual @MongoId private String key; } public class Friend { // auto @MongoObjectId private String _id; } public class Friend { @MongoId // auto @MongoObjectId private String key; } public class Friend { // auto private ObjectId _id; }
This implementation is not perfect.You can find how to use it by, following the Example class and the source code in github:https://github.com/ThilinaManamgoda/Mongodb-Jongo-JAVA
If your are interested please help me to to make this better cheers !!!