mongodb driver使用代碼詳解
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由 C++ 語(yǔ)言編寫。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。
0 前言
全是干貨的技術(shù)殿堂
文章收錄在我的 GitHub 倉(cāng)庫(kù),歡迎Star/fork:
Java-Interview-Tutorial
https://github.com/Wasabi1234/Java-Interview-Tutorial
mongodb-driver是mongo官方推出的java連接mongoDB的驅(qū)動(dòng)包,相當(dāng)于JDBC驅(qū)動(dòng)。我們現(xiàn)在來(lái)使用mongodb-driver
完成對(duì)Mongodb的操作。
1 環(huán)境準(zhǔn)備
創(chuàng)建工程,并添加以下依賴:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.10.1</version> </dependency>
2 使用mongodb-driver
2.1 查詢所有
@Test public void test1() { //創(chuàng)建連接 MongoClient client = new MongoClient("192.168.200.128"); //打開(kāi)數(shù)據(jù)庫(kù) MongoDatabase commentdb = client.getDatabase("commentdb"); //獲取集合 MongoCollection<Document> comment = commentdb.getCollection("comment"); //查詢 FindIterable<Document> documents = comment.find(); //查詢記錄獲取文檔集合 for (Document document : documents) { System.out.println("_id:" + document.get("_id")); System.out.println("內(nèi)容:" + document.get("content")); System.out.println("用戶ID:" + document.get("userid")); System.out.println("點(diǎn)贊數(shù):" + document.get("thumbup")); } //關(guān)閉連接 client.close(); } }
2.2 根據(jù)_id查詢
每次使用都要用到MongoCollection
,進(jìn)行抽?。?/p>
private MongoClient client; private MongoCollection<Document> comment; @Before public void init() { //創(chuàng)建連接 client = new MongoClient("192.168.200.128"); //打開(kāi)數(shù)據(jù)庫(kù) MongoDatabase commentdb = client.getDatabase("commentdb"); //獲取集合 comment = commentdb.getCollection("comment"); } @After public void after() { client.close(); } @Test public void test2() { //查詢 FindIterable<Document> documents = comment.find(new BasicDBObject("_id", "1")); //查詢記錄獲取文檔集合 for (Document document : documents) { System.out.println("_id:" + document.get("_id")); System.out.println("內(nèi)容:" + document.get("content")); System.out.println("用戶ID:" + document.get("userid")); System.out.println("點(diǎn)贊數(shù):" + document.get("thumbup")); } }
2.3 新增
@Test public void test3() { Map<String, Object> map = new HashMap(); map.put("_id", "6"); map.put("content", "很棒!"); map.put("userid", "9999"); map.put("thumbup", 123); Document document = new Document(map); comment.insertOne(document); }
2.4 修改
@Test public void test4() { //修改的條件 Bson filter = new BasicDBObject("_id", "6"); //修改的數(shù)據(jù) Bson update = new BasicDBObject("$set", new Document("userid", "8888")); comment.updateOne(filter, update); }
2.5 刪除
@Test public void test5() { //刪除的條件 Bson filter = new BasicDBObject("_id", "6"); comment.deleteOne(filter); }
MongoDB優(yōu)勢(shì)與劣勢(shì)
優(yōu)勢(shì):
1、在適量級(jí)的內(nèi)存的MongoDB的性能是非常迅速的,它將熱數(shù)據(jù)存儲(chǔ)在物理內(nèi)存中,使得熱數(shù)據(jù)的讀寫變得十分快。
2、MongoDB的高可用和集群架構(gòu)擁有十分高的擴(kuò)展性。
3、在副本集中,當(dāng)主庫(kù)遇到問(wèn)題,無(wú)法繼續(xù)提供服務(wù)的時(shí)候,副本集將選舉一個(gè)新的主庫(kù)繼續(xù)提供服務(wù)。
4、MongoDB的Bson和JSon格式的數(shù)據(jù)十分適合文檔格式的存儲(chǔ)與查詢。
劣勢(shì):
1、 不支持事務(wù)操作。MongoDB本身沒(méi)有自帶事務(wù)機(jī)制,若需要在MongoDB中實(shí)現(xiàn)事務(wù)機(jī)制,需通過(guò)一個(gè)額外的表,從邏輯上自行實(shí)現(xiàn)事務(wù)。
2、 應(yīng)用經(jīng)驗(yàn)少,由于NoSQL興起時(shí)間短,應(yīng)用經(jīng)驗(yàn)相比關(guān)系型數(shù)據(jù)庫(kù)較少。
3、MongoDB占用空間過(guò)大。
總結(jié)
到此這篇關(guān)于mongodb driver使用代碼詳解的文章就介紹到這了,更多相關(guān)mongodb driver使用 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Mongodb參數(shù)說(shuō)明與常見(jiàn)錯(cuò)誤處理的總結(jié)
這篇文章主要給大家介紹了關(guān)于Mongodb參數(shù)說(shuō)明與常見(jiàn)錯(cuò)誤處理的相關(guān)資料,文中通過(guò)一步步的步驟介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-07-07Mongodb啟動(dòng)命令參數(shù)中文說(shuō)明
這篇文章主要介紹了Mongodb啟動(dòng)命令參數(shù)中文說(shuō)明,本文包括基本配置、主/從參數(shù)、Sharding(分片)選項(xiàng)等內(nèi)容,需要的朋友可以參考下2014-10-10Ubuntu中安裝MongoDB及執(zhí)行一些簡(jiǎn)單操作筆記
這篇文章主要介紹了Ubuntu中安裝MongoDB及執(zhí)行一些簡(jiǎn)單操作筆記,本文同時(shí)給出了查看已有數(shù)據(jù)庫(kù)、刪除數(shù)據(jù)庫(kù)、創(chuàng)建數(shù)據(jù)庫(kù)等操作命令實(shí)例,需要的朋友可以參考下2014-09-09Mongodb數(shù)據(jù)庫(kù)的備份與恢復(fù)操作實(shí)例
這篇文章主要介紹了Mongodb數(shù)據(jù)庫(kù)的備份與恢復(fù)操作實(shí)例,本文講解使用命令在控制臺(tái)執(zhí)行實(shí)現(xiàn)Mongodb的備份與恢復(fù)操作,需要的朋友可以參考下2015-01-01Windows系統(tǒng)安裝運(yùn)行Mongodb服務(wù)
今天小編就為大家分享一篇關(guān)于Windows系統(tǒng)安裝運(yùn)行Mongodb服務(wù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10MongoDB 3.4配置文件避免入坑的注意事項(xiàng)
最近在配置mongodb的時(shí)候遇到了一些問(wèn)題,現(xiàn)總結(jié)出來(lái)方便以后需要或同樣遇到該問(wèn)題的朋友們參考,下面這篇文章主要給大家介紹了關(guān)于MongoDB 3.4配置文件時(shí)避免入坑的兩個(gè)注意事項(xiàng),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09