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);
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.
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/
-
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");
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.