国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MongoDB > 正文

mongodb driver使用代碼詳解

2020-10-29 18:40:40
字體:
來源:轉載
供稿:網友

MongoDB 是一個基于分布式文件存儲的數據庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數據存儲解決方案。

MongoDB 是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。

0 前言

全是干貨的技術殿堂

文章收錄在我的 GitHub 倉庫,歡迎Star/fork:

Java-Interview-Tutorial

https://github.com/Wasabi1234/Java-Interview-Tutorial

mongodb-driver是mongo官方推出的java連接mongoDB的驅動包,相當于JDBC驅動。我們現在來使用mongodb-driver完成對Mongodb的操作。

1 環境準備

創建工程,并添加以下依賴:

<dependency>  <groupId>org.mongodb</groupId>  <artifactId>mongodb-driver</artifactId>  <version>3.10.1</version> </dependency>

2 使用mongodb-driver

2.1 查詢所有

@Test public void test1() {  //創建連接  MongoClient client = new MongoClient("192.168.200.128"); //打開數據庫  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("內容:" + document.get("content"));  System.out.println("用戶ID:" + document.get("userid"));  System.out.println("點贊數:" + document.get("thumbup")); } //關閉連接  client.close();  } } 

2.2 根據_id查詢

每次使用都要用到MongoCollection,進行抽取:

private MongoClient client; private MongoCollection<Document> comment; @Before public void init() {  //創建連接  client = new MongoClient("192.168.200.128");  //打開數據庫  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("內容:" + document.get("content"));  System.out.println("用戶ID:" + document.get("userid"));  System.out.println("點贊數:" + 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");  //修改的數據  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優勢與劣勢

優勢:

1、在適量級的內存的MongoDB的性能是非常迅速的,它將熱數據存儲在物理內存中,使得熱數據的讀寫變得十分快。
2、MongoDB的高可用和集群架構擁有十分高的擴展性。
3、在副本集中,當主庫遇到問題,無法繼續提供服務的時候,副本集將選舉一個新的主庫繼續提供服務。
4、MongoDB的Bson和JSon格式的數據十分適合文檔格式的存儲與查詢。

劣勢:

1、 不支持事務操作。MongoDB本身沒有自帶事務機制,若需要在MongoDB中實現事務機制,需通過一個額外的表,從邏輯上自行實現事務。
2、 應用經驗少,由于NoSQL興起時間短,應用經驗相比關系型數據庫較少。
3、MongoDB占用空間過大。

總結

到此這篇關于mongodb driver使用代碼詳解的文章就介紹到這了,更多相關mongodb driver使用 內容請搜索武林網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 偃师市| 海晏县| 临邑县| 宽甸| 息烽县| 罗甸县| 南木林县| 清水河县| 竹溪县| 准格尔旗| 景德镇市| 延安市| 工布江达县| 阳新县| 乡城县| 辉南县| 积石山| 庐江县| 穆棱市| 秦皇岛市| 五华县| 图木舒克市| 晋城| 安仁县| 双辽市| 枣阳市| 罗甸县| 苍溪县| 杭锦后旗| 油尖旺区| 伊川县| 陇川县| 仁怀市| 湖州市| 清流县| 河津市| 长葛市| 河东区| 武宣县| 彭阳县| 博客|