You are on page 1of 7

Assignment-2(Group-B)

Title: Implement database using MongoDB


Aim: Design database with suitable example using MongoDB and
implement all basic operations and administration commands using two
tier architecture.
Prerequisites:
Basic of MongoDB(Basic commands)
Basic of Java(Classes MongoDB library)
Objectives: Ability of problem solving using advanced databases techniques
and
tools
Theory:
Installation
Before we start using MongoDB in our Java programs, we need to make
sure that we have MongoDB JDBC Driver and Java set up on the machine. You
need to download the jar from the path
http://central.maven.org/maven2/org/mongodb/mongo-javadriver/2.11.3/.
Make sure to download latest release of it.

You need to include the mongo.jar into your classpath.

Steps to install MongoDB on Windows


1. Download MongoDB (mongodb-win32-x86_64-2008plus-2.7.3.zip)
2. Create folder MongoDb on C:/ drive
3. Extract Mongodb in MongoDB folder
4. Make Directory db in data C:\data\db
5. Go to the command prompt
6. Start Server C:\MongoDB\bin\mongod.exe

Using MongoShell
1.
2.
3.
4.
5.

Go to the command prompt


C:\MongoDB\bin\mongo.exe
Showdata
db;
test database

6. use spcoe;
7. switch to db spcoe;
Connect to database:
To connect database, you need to specify database name, if database
doesn't exist then mongodb creates it automatically.
Classes in mongo-java-driver.jar:
1.DB:(public abstract class DB extends
Object) Package: com.mongodb.DB
An abstract class that represents a logical database on a server
Constructor
s:
DB( Mongo mongo, String name)
Method Summary:
Modifier
and

Method

Descriptio
n

Type
void

addOption(int option)

Adds the give option

DBCollectio
n

Gets a collection with a


getCollection( String name) given
name.

Set<
String>

getCollectionFromString
(String s)

Gets a collection with a


given
name.

Mongo

getMongo()

createCollection( String
DBCollection name,
DBObject options)
boolean

Gets the Mongo


instance
Creates a collection with
a
given name and
options

authenticate( String
username,

Authenticat
e
database user

char[] password)

with given username and


password

void

dropDatabase()

Drops this database

2. DBCollection: (public abstract class DBCollection


extends Object) Package: com.mongodb.DBCollection
This class provides a skeleton implementation of a database collection

Constructors :
DBCollection(DB base, String name)
Method Summary:
Modifier

Method

Descriptio
n

and Type
long

count()

returns the number of documents


in
this collection.

void

createIndex( DBObject
keys)
calls
createIndex(com.mongodb.DB
Object,
com.mongodb.DBObject)
default index options

void

DBCursor

drop()

Drops (deletes) this


collection.

find( DBObject ref)

Queries for an object in this


collection.

WriteResu
insert( DBObject... arr)
lt

Saves document(s) to the


database.

WriteResu
remove( DBObject o)
lt

calls

with

remove(com.mongodb.DBObje
ct,
com.mongodb.WriteConc
ern)

with

the default WriteConcern

3. MongoClient: (public class MongoClient extends


Mongo) Package: com.mongodb.MongoClient
A MongoDB client with internal connection pooling. For most
applications, you should have one MongoClient instance for the entire JVM.

Constructors :
MongoClient() : Creates an instance based on a (single) mongodb node
(localhost,
default port).

MongoClient( String host, int port): Creates a Mongo instance based on a


(single)
mongodb node.
Parameters:
host - the database's host address
port - the port on which the database is running
4. BasicDBObject: (public class BasicDBObject extends
BasicBSONObject implements DBObject)
Package: com.mongodb.BasicDBObject
A basic implementation of bson object that is mongo specific. A
DBObject can be created as follows, using this class:
DBObject obj = new

BasicDBObject(); obj.put( "foo",


"bar" );

Constructors :
public BasicDBObject(int size): creates an empty object
public BasicDBObject(int size): creates an empty object

5. DBCursor: (public class DBCursor extends Object implements


Iterator<DBObject>, Iterable<DBObject>, Closeable)
Package: com.mongodb. DBCursor
An iterator over database results. Doing a find() query on a collection
returns a DBCursor thus :
DBCursor cursor =
collection.find( query );
if( cursor.hasNext() )
DBObject obj = cursor.next();

Constructors :
public DBCursor(DBCollection collection,DBObject q,
DBObject k, ReadPreference preference)): Initializes
a new database cursor

Method Summary:
Modifier and Method

Description

Type
public booleanhasNext()

Checks if there is another object

available
public DBObject

next()

Returns the object the cursor is


at
and moves the cursor ahead
by
one.

public void
RDBMS

MongoDB

Database

Database

Table

Collection

Tuple/Row

Document

column

Field

Table Join

Embedded Documents

Primary Key
RDBMS

public int

Primary Key (Default key

_id provided by

mongodb itself)
MongoDB

MongoDB Overview:
MongoDB is a cross-platform, document oriented database that
provides,

high

performance,

high

availability,

and

easy

scalability.

MongoDB works on concept of collection and document.


Database
Database is a physical container for collections. Each database gets
its own set of files on the file system. A single MongoDB server typically
has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of
an RDBMS table. A collection exists within a single database. Collections do
not enforce a schema. Documents within a collection can have different
fields. Typically, all documents in a collection are of similar or related
purpose.
Document
A document is a set of key-value pairs. Documents have dynamic
schema. Dynamic schema means that documents in the same collection do
not need to have the same set of fields or structure, and common fields in
a collection's documents may hold different types of data.
Below given table shows the relationship of RDBMS terminology with
MongoDB
Sample document

Below given example shows the document structure of a blog site which is
simply a comma separated key value pair.
{
_id:
ObjectId(7df78ad8902c)
title: 'MongoDB Overview',

description: 'MongoDB is no sql


database', by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags:

['mongodb',

'database',

'NoSQL'], likes: 100,


}

_id is a 12 bytes hexadecimal number which assures the uniqueness


of every document. You can provide _id while inserting the document. If you
didn't provide then MongoDB provide a unique id for every document. These
12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id,
next 2 bytes for process id of mongodb server and remaining 3 bytes are
simple incremental value.
Conclusion:
Here we performed Connectivity with MongoDB using Java
application(Two
tier).

You might also like