mongo 使用手冊

最近做項目想用一下NoSQL數據庫,由於項目需要保存大量的json數據,我就選了MongoDB作爲我的數據庫。

最新版爲3.0 下載安裝都很容易,今天按照教程一步一步實現了增刪改查等工作,本文將把要用到的代碼都貼上來而且都做好中文註釋方便大家閱讀。

過程中發現兩處不能使用教程中的代碼實現的地方,經過查資料已經用另外的方法實現了,不知道是不是叫承重的代碼出錯了還是什麼原因,如果有知道的朋友請留言告訴我,多謝~

 

官方教程鏈接:http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/

 

如果不用maven附件中提供了已經下好的java驅動

 

Java代碼  收藏代碼
  1. package com.zhongli.TwitterGetter.app;  
  2.   
  3. import java.util.*;  
  4.   
  5. import org.bson.Document;  
  6. import org.bson.conversions.Bson;  
  7.   
  8. import com.mongodb.BasicDBObject;  
  9. import com.mongodb.Block;  
  10. import com.mongodb.MongoClient;  
  11. import com.mongodb.client.*;  
  12. import com.mongodb.client.model.BulkWriteOptions;  
  13. import com.mongodb.client.model.DeleteOneModel;  
  14. import com.mongodb.client.model.InsertOneModel;  
  15. import com.mongodb.client.model.ReplaceOneModel;  
  16. import com.mongodb.client.model.UpdateOneModel;  
  17. import com.mongodb.client.result.DeleteResult;  
  18. import com.mongodb.client.result.UpdateResult;  
  19.   
  20. import static com.mongodb.client.model.Filters.*;  
  21.   
  22. /** 
  23.  * 程序入口 
  24.  *  
  25.  * @author John 
  26.  * 
  27.  */  
  28. public class testmain {  
  29.     public static void main(String[] args) {  
  30.         testmain tm = new testmain();  
  31.         tm.test();  
  32.     }  
  33.   
  34.     /** 
  35.      * test 
  36.      */  
  37.     private void test() {  
  38.         // 獲取鏈接  
  39.         MongoClient mongoClient = new MongoClient("localhost"27017);  
  40.         // 獲取數據庫  
  41.         MongoDatabase database = mongoClient.getDatabase("mydb");  
  42.         // 進入某個文檔集  
  43.         MongoCollection<Document> collection = database.getCollection("test");  
  44.   
  45.         /********************** 數據插入 ****************************/  
  46.         // // 創建新文檔  
  47.         // Document doc = new Document("name", "MongoDB")  
  48.         // .append("type", "database").append("count", 1)  
  49.         // .append("info", new Document("x", 203).append("y", 102));  
  50.         // // 將文檔插入文檔集合  
  51.         // collection.insertOne(doc);  
  52.         //  
  53.         // // 創建一個包含多個文檔的列表  
  54.         // List<Document> documents = new ArrayList<Document>();  
  55.         // for (int i = 0; i < 100; i++) {  
  56.         // documents.add(new Document("i", i));  
  57.         // }  
  58.         // // 向文檔中插入列表  
  59.         // collection.insertMany(documents);  
  60.   
  61.         /***************** 數據讀取 ****************************************/  
  62.         // // 顯示集合中的文檔的數量  
  63.         // System.out.println(collection.count());  
  64.         //  
  65.         // // 查詢集合中的第一個文檔  
  66.         // Document myDoc = collection.find().first();  
  67.         // System.out.println(myDoc.toJson());  
  68.         //  
  69.         // //獲取集合中的全部文檔  
  70.         // MongoCursor<Document> cursor = collection.find().iterator();  
  71.         // try {  
  72.         // while (cursor.hasNext()) {  
  73.         // System.out.println(cursor.next().toJson());  
  74.         // }  
  75.         // } finally {  
  76.         // cursor.close();  
  77.         // }  
  78.   
  79.         // //獲取全部文檔的另一種方法  
  80.         // for (Document cur : collection.find()) {  
  81.         // System.out.println(cur.toJson());  
  82.         // }  
  83.   
  84.         // // 根據條件獲取某分文檔 eq:==  
  85.         // Document myDoc = collection.find(eq("i", 71)).first();  
  86.         // System.out.println(myDoc.toJson());  
  87.   
  88.         // 通過查詢語句一次性獲取多個數據  
  89.         // Block<Document> printBlock = new Block<Document>() {  
  90.         // @Override  
  91.         // public void apply(final Document document) {  
  92.         // System.out.println(document.toJson());  
  93.         // }  
  94.         // };  
  95.         // 獲得所有大於50的  
  96.         // collection.find(gt("i", 50)).forEach(printBlock);  
  97.         // 大於50 小於 100  
  98.         // collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock);  
  99.   
  100.         // 對輸出文檔進行排序,-1爲遞減,1爲遞增  
  101.         // 官方文檔的例子有誤:http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/#sorting-documents  
  102.         // Document myDoc = collection.find(exists("i"))  
  103.         // .sort(new BasicDBObject("i", -1)).first();  
  104.         // System.out.println(myDoc.toJson());  
  105.   
  106.         // 選擇性輸出結果中的元素,0爲不顯示,1爲顯示  
  107.         // 官方文檔中的例子又不能用:http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/#projecting-fields  
  108.         // BasicDBObject exclude = new BasicDBObject();  
  109.         // exclude.append("_id", 0);  
  110.         // // exclude.append("count", 0);  
  111.         // exclude.append("name", 1);  
  112.         // exclude.append("info", 1);  
  113.         // Document myDoc = collection.find().projection(exclude).first();  
  114.         // System.out.println(myDoc.toJson());  
  115.   
  116.         /************************* 修改數據庫中數據 *************************************/  
  117.   
  118.         // 修改時的參數:  
  119.         // $inc 對指定的元素加  
  120.         // $mul 乘  
  121.         // $rename 修改元素名稱  
  122.         // $setOnInsert 如果以前沒有這個元素則增加這個元素,否則不作任何更改  
  123.         // $set 修改制定元素的值  
  124.         // $unset 移除特定的元素  
  125.         // $min 如果原始數據更大則不修改,否則修改爲指定的值  
  126.         // $max 與$min相反  
  127.         // $currentDate 修改爲目前的時間  
  128.   
  129.         // //修改第一個符合條件的數據  
  130.         // $set 爲修改  
  131.         // collection.updateOne(eq("i", 10), new Document("$set", new  
  132.         // Document("i", 110)));  
  133.         // // 獲取全部文檔,可以看到以前10的地方變成了110  
  134.         // for (Document cur : collection.find()) {  
  135.         // System.out.println(cur.toJson());  
  136.         // }  
  137.   
  138.         // 批量修改數據並且返回修改的結果,講所有小於100的結果都加100  
  139.         // UpdateResult updateResult = collection.updateMany(lt("i", 100),  
  140.         // new Document("$inc", new Document("i", 100)));  
  141.         // // 顯示發生變化的行數  
  142.         // System.out.println(updateResult.getModifiedCount());  
  143.         // // 獲取全部文檔,可以看到除了剛纔修改的110其他的全爲了100  
  144.         // for (Document cur : collection.find()) {  
  145.         // System.out.println(cur.toJson());  
  146.         // }  
  147.   
  148.         /************************** 刪除數據 *****************************/  
  149.         // 刪除第一個符合條件的數據  
  150.         // collection.deleteOne(eq("i", 110));  
  151.         // // 獲取全部文檔,可以看到沒有110這個數了  
  152.         // for (Document cur : collection.find()) {  
  153.         // System.out.println(cur.toJson());  
  154.         // }  
  155.   
  156.         // 刪除所有符合條件的數據,並且返回結果  
  157.         // DeleteResult deleteResult = collection.deleteMany(gte("i", 100));  
  158.         // // 輸出刪除的行數  
  159.         // System.out.println(deleteResult.getDeletedCount());  
  160.         // // 獲取全部文檔,所有i>=100的數據都沒了  
  161.         // for (Document cur : collection.find()) {  
  162.         // System.out.println(cur.toJson());  
  163.         // }  
  164.         /*************************** 程序塊,一次執行多條語句 ********************************/  
  165.         // 按照語句先後順序執行  
  166.         // collection.bulkWrite(Arrays.asList(new InsertOneModel<>(new Document(  
  167.         // "_id", 4)), new InsertOneModel<>(new Document("_id", 5)),  
  168.         // new InsertOneModel<>(new Document("_id", 6)),  
  169.         // new UpdateOneModel<>(new Document("_id", 1), new Document(  
  170.         // "$set", new Document("x", 2))), new DeleteOneModel<>(  
  171.         // new Document("_id", 2)),  
  172.         // new ReplaceOneModel<>(new Document("_id", 3), new Document(  
  173.         // "_id", 3).append("x", 4))));  
  174.         // // 獲取全部文檔  
  175.         // for (Document cur : collection.find()) {  
  176.         // System.out.println(cur.toJson());  
  177.         // }  
  178.   
  179.         // 不按照語句先後順序執行  
  180.         // collection.bulkWrite(Arrays.asList(new InsertOneModel<>(new Document(  
  181.         // "_id", 4)), new InsertOneModel<>(new Document("_id", 5)),  
  182.         // new InsertOneModel<>(new Document("_id", 6)),  
  183.         // new UpdateOneModel<>(new Document("_id", 1), new Document(  
  184.         // "$set", new Document("x", 2))), new DeleteOneModel<>(  
  185.         // new Document("_id", 2)),  
  186.         // new ReplaceOneModel<>(new Document("_id", 3), new Document(  
  187.         // "_id", 3).append("x", 4))), new BulkWriteOptions()  
  188.         // .ordered(false));  
  189.         // 獲取全部文檔  
  190.         // for (Document cur : collection.find()) {  
  191.         // System.out.println(cur.toJson());  
  192.         // }  
  193.           
  194.           
  195.         // 關閉數據庫連接  
  196.         mongoClient.close();  
  197.   
  198.     }  
  199.   
  200. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章