http://vietjack.com/mongodb/index.jsp Copyright © vietjack.com MongoDB với Java Cài đặt Trước bắt đầu sử dụng MongoDB chương trình Java, bạn cần đảm bảo có MongoDB JDBC Driver Java cài đặt máy Bạn vào loạt Java để cài đặt Java máy cho Bây giờ, cần cài đặt MongoDB JDBC Driver • Bạn cần tải jar từ Tải mongo.jar Bạn nên tải phiên • Bạn cần bao mongo.jar vào Classpath Kết nối tới Database Để kết nối Database, bạn cần xác định tên sở liệu đó, sở liệu khơng tồn tại, MongoDB tự động tạo Dưới code để kết nối Database: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Biên dịch chạy chương trình để tạo sở liệu Bạn thay đổi path tùy theo yêu cầu Giả sử phiên JDBC Driver mongo-2.10.1.jar có sẵn path $javac MongoDBJDBC.java $java -classpath ".:mongo-2.10.1.jar" MongoDBJDBC Connect to database successfully Authentication: true Nếu bạn sử dụng Windows, bạn biên dịch chạy code sau: $javac MongoDBJDBC.java $java -classpath ".;mongo-2.10.1.jar" MongoDBJDBC Connect to database successfully Authentication: true http://vietjack.com/ Trang chia sẻ các bài học online miễn phí http://vietjack.com/mongodb/index.jsp Copyright © vietjack.com Giá trị auth true, username password hợp lệ cho Database chọn Tạo Collection Để tạo Collection, bạn sử dụng phương thức createCollection() lớpcom.mongodb.DB Bạn theo dõi code sau: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); DBCollection coll = db.createCollection("mycol"); System.out.println("Collection created successfully"); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Khi chương trình biên dịch thực thi, kết là: Connect to database successfully Authentication: true Collection created successfully Lấy/chọn Collection Để lấy/chọn Collection từ Database, bạn sử dụng phương thức getCollection() lớpcom.mongodb.DBCollection Bạn theo dõi code sau: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); http://vietjack.com/ Trang chia sẻ các bài học online miễn phí http://vietjack.com/mongodb/index.jsp Copyright © vietjack.com DBCollection coll = db.createCollection("mycol"); System.out.println("Collection created successfully"); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Khi chương trình biên dịch thực thi, kết là: Connect to database successfully Authentication: true Collection created successfully Collection mycol selected successfully Chèn Document Để chèn Document vào MongoDB, bạn sử dụng phương thức insert() lớpcom.mongodb.DBCollection Bạn theo dõi code sau: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); BasicDBObject doc = new BasicDBObject("title", "MongoDB") append("description", "database") append("likes", 100) append("url", "http://www.tutorialspoint.com/mongodb/") append("by", "tutorials point"); coll.insert(doc); System.out.println("Document inserted successfully"); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Khi chương trình biên dịch thực thi, kết là: Connect to database successfully Authentication: true Collection mycol selected successfully Document inserted successfully http://vietjack.com/ Trang chia sẻ các bài học online miễn phí http://vietjack.com/mongodb/index.jsp Copyright © vietjack.com Lấy tất Document Để chọn tất Document từ Collection, bạn sử dụng phương thức find() lớpcom.mongodb.DBCollection Phương thức trả trỏ, bạn cần lặp qua trỏ Bạn theo dõi code sau: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); DBCursor cursor = coll.find(); int i=1; while (cursor.hasNext()) { System.out.println("Inserted Document: "+i); System.out.println(cursor.next()); i++; } }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Khi chương trình biên dịch thực thi, kết là: Connect to database successfully Authentication: true Collection mycol selected successfully Inserted Document: { "title": "MongoDB", "_id" : ObjectId(7df78ad8902c), "description": "database", "http://www.tutorialspoint.com/mongodb/", "likes": 100, "url": "by": "tutorials point" } Cập nhật Document Để cập nhật Document từ Collection, bạn sử dụng phương thức update() lớpcom.mongodb.DBCollection Bạn theo dõi code sau: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to http://vietjack.com/ Trang chia sẻ các bài học online miễn phí http://vietjack.com/mongodb/index.jsp Copyright © vietjack.com mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); DBCursor cursor = coll.find(); while (cursor.hasNext()) { DBObject updateDocument = cursor.next(); updateDocument.put("likes","200") col1.update(updateDocument); } System.out.println("Document updated successfully"); cursor = coll.find(); int i=1; while (cursor.hasNext()) { System.out.println("Updated Document: "+i); System.out.println(cursor.next()); i++; } }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Khi chương trình biên dịch thực thi, kết là: Connect to database successfully Authentication: true Collection mycol selected successfully Document updated successfully Updated Document: { ObjectId(7df78ad8902c), "likes": 100, "title": "MongoDB", "_id" : "description": "database", "url": "http://www.tutorialspoint.com/mongodb/", "by": "tutorials point" } Xóa Document Để xóa Document từ Collection, bạn cần chọn Document sử dụng phương thức findOne() sau sử dụng phương thức remove lớpcom.mongodb.DBCollection Bạn theo dõi code sau: import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; public class MongoDBJDBC{ public static void main( String args[] ){ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate(myUserName, myPassword); System.out.println("Authentication: "+auth); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); DBObject myDoc = coll.findOne(); col1.remove(myDoc); DBCursor cursor = coll.find(); int i=1; while (cursor.hasNext()) { System.out.println("Inserted Document: "+i); http://vietjack.com/ Trang chia sẻ các bài học online miễn phí http://vietjack.com/mongodb/index.jsp Copyright © vietjack.com System.out.println(cursor.next()); i++; } System.out.println("Document deleted successfully"); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } } Khi chương trình biên dịch thực thi, kết là: Connect to database successfully Authentication: true Collection mycol selected successfully Document deleted successfully Các phương thức save(), limit(), skip(), sort(), MongoDB làm việc tương tự giải thích http://vietjack.com/ Trang chia sẻ các bài học online miễn phí ... import com .mongodb. DB; import com .mongodb. DBCollection; import com .mongodb. BasicDBObject; import com .mongodb. DBObject; import com .mongodb. DBCursor; import com .mongodb. ServerAddress; import java. util.Arrays;... import com .mongodb. DB; import com .mongodb. DBCollection; import com .mongodb. BasicDBObject; import com .mongodb. DBObject; import com .mongodb. DBCursor; import com .mongodb. ServerAddress; import java. util.Arrays;... lớpcom .mongodb. DBCollection Bạn theo dõi code sau: import com .mongodb. MongoClient; import com .mongodb. MongoException; import com .mongodb. WriteConcern; import com .mongodb. DB; import com .mongodb. DBCollection;