mongo 使用手册

来源:互联网 发布:sql时间值对比 编辑:程序博客网 时间:2024/05/17 03:09

最近做项目想用一下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. }  
0 0