WELCOME Abdennour : Software engineer

Apr 9, 2012

MongoDB Helper (Hibernate For MongoDB with Factory Pattern ) V1.0.1



1.Introduction : 
                         
  •  MongoDB  is an open source document-oriented NoSQL database system.(From en.wikipedia.org)
  •  MongoDB is written in C++, with many great features like map-reduce , auto sharding, replication, high availability and etc.(From mkyong.com)

Difference between collection & Table:
   The element of data is called documents, stored in collections. One collection may have any number of documents.
==> collections are as tables
==> documents are as records.
2.Installation Of MongoDB:
2.a.Install MongoDB
 2.a.Install MongoDB on Windows:
§  How to install MongoDB on Windows.
 2)Unzip: (mongodb-win32-1.8.1)

I.SERVER

3) create a data directory for MongoDB:Exple(C:\mymongodb)
 =>.\bin\mongod.exe : is the DB server.
      4)  run this DB Server and specify  the data directory by “ dbpath
è$.\mongodb-win32-1.8.1\bin>mongod --dbpath "c:\mymongodb"
      5)Admin the Server by Web Interface : http://your-ipaddress:28017 (Example: http://localhost:28017 )

II.Client :

          6)Connect as Client By this command :
 $.\bin>mongo
        
§  Run MongoDB as Windows Service
  1. mongod –help
Get to know all the Windows service related commands by typing “mongod –help“.
C:\MongoDB\bin>mongod --help
 
èThe “–install” and “–remove” commands are what you need.
§  To install as Windows service :
è$#> mongod --dbpath "D:\gl4\formation\5.mongoDB\mymongodb" --logpath "D:\gl4\formation\5.mongoDB\mymongodb\logs.txt" --install --serviceName "MongoDB"
The Cmd Line means :
    a.Install “MongoDb” service  which point to D:\gl4\formation\5.mongoDB\mymongodb.
    b.Logs Outputs is the file : D:\gl4\formation\5.mongoDB\mymongodb\logs.txt
To uninstall :* #> mongod --remove --serviceName "MongoDB"

2.b.Install MongoDB on Ubuntu

3.  MongoDBFactoryHelper FrameWork: 

mongoDBHelper is an API based on the pattern "Factory", which helps the developer in getting rid of the awkward handling of mongDB.In effect, we will show you how you exploit it.

=>

3.1. Download Jar File : 
MongoDBFatoryHelper ,Congratulations!!! now you can follow the rest of tutorial
3.2. reminder : 

Don' forget to start server &client. 

  • Run Server : $#../bin> mongod --dbpath "D:\gl4\formation\5.mongoDB\mymongodb" 
  • Run Client : $#./bin> mongo

      IF you have Amiguity , follow the installations' Tutos of MongoBD
3.2. Embed the FrameWork in a Eclipse Project 



3.3. Test CRUD Methods : CREATE


TableProduct doc1 = new TableProduct();

    doc1.setTableFactory(tableFactory);

    doc1.setValues(Arrays.asList("1""Abdennour""Toumi""32534"));



    TableProduct doc2 = new TableProduct();

    doc2.setTableFactory(tableFactory);

    doc2.setValues(Arrays.asList("2""Rami""RRRR""1114"));



    TableProduct doc3 = new TableProduct();

    doc3.setTableFactory(tableFactory);

    doc3.setValues(Arrays.asList("3""Ali""Iyath""2224"));



    crud.insert(doc1);

    crud.insert(doc2);

    crud.insert(doc3);



3.4. Test CRUD Methods : READ:


System.out.println(crud.findAll().toString());



3.5. Full Example:


package tess;



import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;



import slm.abdennour.DataAcess.MonGo.beans.TableFactory;

import slm.abdennour.DataAcess.MonGo.beans.TableProduct;

import slm.abdennour.DataAcess.MonGo.impl.CRUDMongo;

import slm.abdennour.DataAcess.MonGo.impl.MongoFactoryBuilder;



public class TestPremierVersion {

  public static CRUDMongo crud;

  public static MongoFactoryBuilder myFactory;

  public static TableFactory tableFactory;



  public static void main(String[] args) {

    // Prepare DB Mongo

    // The Fields Of Tables

    myFactory = new MongoFactoryBuilder("dbtestFor""clperson");

    // Fields of Table

    List<String> argsTab = Arrays.asList("id""firstname""lastname",

        "telephone");

    tableFactory = myFactory.getTableByName("person", argsTab);

    crud = myFactory.getSession();

    testDummyInsert();

         testFindAll();

    



  }



  public static void testDummyInsert() {

    // Prepare Documents

    TableProduct doc1 = new TableProduct();

    doc1.setTableFactory(tableFactory);

    doc1.setValues(Arrays.asList("1""Abdennour""Toumi""32534"));



    TableProduct doc2 = new TableProduct();

    doc2.setTableFactory(tableFactory);

    doc2.setValues(Arrays.asList("2""Abdesslem""RRRR""1114"));



    TableProduct doc3 = new TableProduct();

    doc3.setTableFactory(tableFactory);

    doc3.setValues(Arrays.asList("3""Ali""Iyath""2224"));



    crud.insert(doc1);

    crud.insert(doc2);

    crud.insert(doc3);



  }



  public static void testFindAll() {

    System.out.println(crud.findAll().toString());

  }



  public static boolean testAuthentification(String login, String passwd) {

    return myFactory.getDBHelper().authentification(login, passwd);

  }

  

  public static void  notYet(){

     //    TestUpdateDelete.testDelete(crud, tableFactory.getFields().get(1), "Rami");

        //   System.out.println("************Show ALL After Delete");

          //testFindAll();

        //  TableProduct newProduct=new TableProduct();

        //  newProduct.setTableFactory(tableFactory);

          //Update Telephone Number

          //newProduct.setValues(Arrays.asList("3", "Ali", "Iyath", "3443"));

         // TestUpdateDelete.testUpdate(crud, newProduct, tableFactory.getFields().get(1), "Ali");

         // System.out.println("************Show ALL After Update");

         // testFindAll();

  }



}




No comments:

Post a Comment