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
[Link]
Make sure to download latest release of it.
You need to include the [Link] into your classpath.
Steps to install MongoDB on Windows
1. Download MongoDB (mongodb-win32-x86_64-[Link])
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\[Link]
Using MongoShell
1.
2.
3.
4.
5.
Go to the command prompt
C:\MongoDB\bin\[Link]
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 [Link]:
[Link]:(public abstract class DB extends
Object) Package: [Link]
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: [Link]
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([Link]
Object,
[Link])
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([Link]
ct,
[Link]
ern)
with
the default WriteConcern
3. MongoClient: (public class MongoClient extends
Mongo) Package: [Link]
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: [Link]
A basic implementation of bson object that is mongo specific. A
DBObject can be created as follows, using this class:
DBObject obj = new
BasicDBObject(); [Link]( "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: [Link]. DBCursor
An iterator over database results. Doing a find() query on a collection
returns a DBCursor thus :
DBCursor cursor =
[Link]( query );
if( [Link]() )
DBObject obj = [Link]();
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: '[Link]
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).