Anda di halaman 1dari 7

MongoDB

Prepared by
: Muhamad Khairul Filhan bin nusi ( JAVA INTERNS)

YOUR
LOGO

CRUD Operations
CRUD Operations

INSERT

Insert documents into objects. Let say we have JSON file format (customer.json)
{
"name" : "Khairul",
"custID" : "A11",
"email" : "i@gmail.com",
"phone" : "0135813392"
}
BasicDBObject note = new BasicDBObject("name", "Khairul")
.append("custID", "A11")
.append("email"," i@gmail.com ")
.append("phone","0135913392");
coll.insert(note);

READ
To prove that the document is inserted,then use :
DBObject myNote = coll.findOne();
System.out.println(myNote);

CRUD perations
UPDATE
BasicDBObject NewMyNote = new BasicDBObject;
NewMyNote.put("Name","Khairul Filhan");
BasicDBObject searchquery = new BasicDBObject().append("Name","Khairul");
collection.update(searchquery,newMyNote);

Find document

DELETE
NewMyNote.append("custID","A11");
collection.remove(NewMyNote);

Remove a document that


contains customer ID of A11

Concepts
ObjectID
- MongoDB defines objectID as an unique ID for specific data. Document stored
require a unique id ( _id ).
- The _id acts as primary key and will be used for querying.
- MongoDB uses objectID as the default value for the _id field if the _id field is not
specified.
- If a client doesnt add _id field, then mongod will add it.
- _id field holds an objectID.

How to make references from one document


to another document.
original_id = ObjectId()
db.places.insert({
"_id": original_id,
"PhoneName": "Galaxy Trend Plus",
"details": "middle-range"
})
db.people.insert({
"PhoneName": "Iphone5",
"places_id": original_id,
"details": "high range "
})

Use _id field of the first document


to the reference in the second document.

t
SORTING
- cursor.sort(sort) specifies the order of returning the matching
documents.
- Ascending/Descending sort { name : 1,phone : -1 }
- MongoDB comparison order refer to website :
http://docs.mongodb.org/manual/reference/method/cursor.sort/
-

BasicDBObject query = new BasicDBObject();


query.put("_id", new BasicDBObject("$lt", "Khairul"));
DBCursor cursor =
collection.find(query).sort(new BasicDBObject("_id","-1"));
for (DBObject db : cursor) {
System.out.println(db);
}

t
Security in MongoDB
- Provides authentication by using MongoDB - CR(challange response).
It authenticates users through passwords.
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("admin");
boolean auth = db.authenticate("myUser", "newPassword".toCharArray());
DB db = mongo.getDB("myData");

Declaration is a must for


every single users

t
SCALING
3 main aspects of scaling :
- Cluster Scale
- Performance Scale
- Data Scale
As the the data growth bigger and bigger,then we can manage and make
it efficient by using 2 methods :
Vertical Scaling - Add more CPU's or resources to increase capacity.
Sharding - Divides and distributes the data set over multiple servers.

Anda mungkin juga menyukai