1、MongoDB for Java的驅動包
https://github.com/mongodb/mongo-java-driver/downloads
2、線上文檔
http://www.mongodb.org/display/DOCS/Java+Language+Center
1、查詢某張表(在MongoDB中稱之為集合)的所有資料
Java代碼DBTest.java
package com.archie.mongodb;import java.net.UnknownHostException;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.Mongo;import com.mongodb.MongoException;/** * 查詢指定資料庫指定DBCollection集合中的所有資料 * @author archie2010 * * since 2012-9-29 下午10:40:21 */public class DBTest { public static void main(String[] args) throws UnknownHostException, MongoException { /** Mongo執行個體代表了一個資料庫連接池 * Mongo mg = new Mongo("localhost"); Mongo mg = new Mongo("localhost", 27017); */ Mongo mg = new Mongo(); // 擷取名為“dbtest”的資料庫物件 DB db = mg.getDB("dbtest"); // 查詢該庫中所有的集合 (相當於表) for (String name : db.getCollectionNames()) { System.out.println("Collection Name: " + name); } DBCollection users = db.getCollection("emp"); // 查詢集合中所有的資料 DBCursor cur = users.find(); System.out.println("Record Count:" + cur.count()); while (cur.hasNext()) { DBObject object = cur.next(); System.out.println(object); // 取出對象中列表為欄位名為'uname'和'upwd'的資料 System.out.println("uname:" + object.get("uname") + "\tupwd:" + object.get("upwd")); } }}
運行結果:
2、對指定DBCollection集合的CRUD操作
Java代碼
DBUtil.java
package com.archie.mongodb;import java.net.UnknownHostException;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.Mongo;/** * 獲得DBCollection集合的工具類 * @author archie2010 * * since 2012-9-29 下午10:54:42 */public class DBUtil { public static Mongo mg=null; public static DB db=null; public static DBCollection collection; /** * 獲得DBCollection對象 * @param dbName * @param colName * @return */ public static DBCollection getDBCollection(String dbName,String colName){ if(mg==null){ try { mg=new Mongo(); } catch (UnknownHostException e) { e.printStackTrace(); } } if(db==null){ db=mg.getDB(dbName); } return db.getCollection(colName); }}
CRUDTest.java
package com.archie.mongodb;import com.mongodb.BasicDBObject;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;/** * 對指定DBCollection集合的CRUD操作 * @author archie2010 * * since 2012-9-29 下午10:51:24 */public class CRUDTest { /** * 增加 * @param obj */ public static void add(DBObject obj){ DBCollection coll=DBUtil.getDBCollection("dbtest", "emp"); coll.insert(obj); } /** * 刪除 * @param obj */ public static void delete(DBObject obj){ DBCollection coll=DBUtil.getDBCollection("dbtest", "emp"); coll.remove(obj); } /** * 查詢 */ public static void query(){ DBCollection coll=DBUtil.getDBCollection("dbtest", "emp"); // 查詢集合中所有的資料 DBCursor cur = coll.find(); System.out.println("Record Count:" + cur.count()); while (cur.hasNext()) { DBObject object = cur.next(); System.out.println(object); // 取出對象中列表為'uname'和'upwd'的資料 System.out.println("uname:" + object.get("uname") + "\tupwd:" + object.get("upwd")+"\t_id:"+object.get("_id")); } } /** * 修改 */ public static void modify(DBObject orig,DBObject update){ DBCollection coll=DBUtil.getDBCollection("dbtest", "emp"); coll.update(orig, update, true, false); } public static void main(String[] args) { DBObject empObj=new BasicDBObject(); empObj.put("uname", "teddy"); empObj.put("upwd", "123456"); //添加 add(empObj); query(); DBObject updateObj=new BasicDBObject(); updateObj.put("uname", "teddy"); updateObj.put("upwd", "3333"); //更新 modify(new BasicDBObject("uname","teddy"),updateObj); System.out.println("-----------------------修改後-------------------"); query(); //刪除 delete(new BasicDBObject("uname","teddy")); System.out.println("-----------------------刪除後-------------------"); query(); }}
運行效果:
樣本下載