7
MongoDB Prepared by : Muhamad Khairul Filhan bin nusi ( JAVA INTERNS) 您的公司名称写在这里 YOUR LOGO

MongoDB and its usage

Embed Size (px)

Citation preview

Page 1: MongoDB and its usage

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

您的公司名称写在这里YOUR LOGO

Page 2: MongoDB and its usage

CRUD Operations

CRUD Operations• INSERT Insert documents into objects. Let say we have JSON file format (customer.json)

{ "name" : "Khairul","custID" : "A11","email" : "[email protected]","phone" : "0135813392"}

BasicDBObject note = new BasicDBObject("name", "Khairul") .append("custID", "A11") .append("email"," [email protected] ") .append("phone","0135913392"); coll.insert(note);

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

Page 3: MongoDB and its usage

CRUD perations

•UPDATEBasicDBObject NewMyNote = new BasicDBObject;NewMyNote.put("Name","Khairul Filhan"); BasicDBObject searchquery = new BasicDBObject().append("Name","Khairul");collection.update(searchquery,newMyNote);

Find document

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

Remove a document thatcontains customer ID of A11

Page 4: MongoDB and its usage

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.

Page 5: MongoDB and its usage

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); }

Page 6: MongoDB and its usage

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 forevery single users

Page 7: MongoDB and its usage

t

•SCALING3 main aspects of scaling : - Cluster Scale- Performance Scale- Data Scale

As the the data growth bigger and bigger,then we can manage and makeit 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.