MongoDB學習(二)MongoDB Java增刪查改

來源:互聯網
上載者:User

  • 相關資料

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

運行效果:

 樣本下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.