MongoDB Java環境下的開發

來源:互聯網
上載者:User

標籤:

        同Mysql、Oracle一樣,首先要下載驅動包,。

        開啟myeclipse,建立一個java project。

        在項目下建立一個lib檔案夾,將下載的驅動包放到lib下並build到path下:

然後在src下建一個db.properties檔案

host=127.0.0.1port=27017dbname=test

建立一個util包,包下建立一個類DBUtil

package util;import java.io.IOException;import java.net.UnknownHostException;import java.util.Properties;import com.mongodb.DBCollection;import com.mongodb.DBObject;import com.mongodb.MongoClient;import com.mongodb.util.JSON;public class DBUtil {private static Properties p=new Properties();private static String host;private static int port;private static String dbname;static{try {p.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));host=(String) p.get("host");port=Integer.valueOf((String) p.get("port"));dbname=(String) p.get("dbname");} catch (IOException e) {e.printStackTrace();}}public static DBCollection getDBCollection(String tablename){try { return new MongoClient(host,port).getDB(dbname).getCollection(tablename);} catch (UnknownHostException e) {e.printStackTrace();}return null;}public static DBObject getDBCursorFromJson(String jsondata){return (DBObject) JSON.parse(jsondata);}public static String getStringDataFromDBObject(DBObject entry){return JSON.serialize(entry);}}

    下面測試簡單的CRUD

package testmongodb;import org.bson.types.ObjectId;import util.DBUtil;import com.mongodb.BasicDBObject;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;public class Test {@org.junit.Testpublic void testAdd(){DBCollection collection=DBUtil.getDBCollection("testtable");DBObject user=new BasicDBObject();user.put("name", "zhangsan");user.put("age", 11);collection.insert(user).getN();}@org.junit.Testpublic void testfind(){//mongoDB不支援聯集查詢、子查詢,這需要我們自己在程式中完成。將查詢的結果集在Java查詢中進行需要的過濾即可。DBCollection collection=DBUtil.getDBCollection("testtable");DBObject user=new BasicDBObject();user.put("name", "zhangsan");user.put("age", "11");//全部查詢//DBCursor cursor=collection.find();//條件查詢DBCursor cursor=collection.find(new BasicDBObject("age", new BasicDBObject("$lte", 105)));while (cursor.hasNext()) {System.out.println(cursor.next());}}@org.junit.Testpublic void testDelete(){DBCollection collection=DBUtil.getDBCollection("testtable");//根據id刪除collection.remove(new BasicDBObject("_id", new ObjectId("5590e57b7d51ad2ef1b69a93")));//條件刪除collection.remove(new BasicDBObject("age", new BasicDBObject("$gte", 105)));}@org.junit.Testpublic void testUpdate(){DBCollection collection=DBUtil.getDBCollection("testtable");//根據id修改collection.update(new BasicDBObject("_id", new ObjectId("5590e57b7d51ad2ef1b69a93")), new BasicDBObject("age", 99));//條件修改collection.update(new BasicDBObject("age", new BasicDBObject("$gte", 105)), new BasicDBObject("age", 11));}}

        關於彙總可以看這篇文章,文章地址

        關於索引可以看這篇文章,文章地址

        最後多嘴一句,MongoDB使用面向對方的方式處理資料庫,讓開發人員不用再去關心sql問題,但也同樣限制了他所能處理的業務,對於複雜的資料分析或者關係比較複雜的erp等,不太適合。不過有一種折中的辦法就是對於要做複雜分析的表依然使用傳統的資料庫,而對於簡單的則用MongoDB。

MongoDB Java環境下的開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.