MongoDB简单测试

pom.xml添加:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- mongo -->  
  2.         <dependency>  
  3.             <groupId>org.mongodb</groupId>  
  4.             <artifactId>mongo-java-driver</artifactId>  
  5.             <version>3.0.2</version>  
  6.         </dependency>  



[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Date;  
  3. import java.util.List;  
  4.   
  5. import org.bson.Document;  
  6.   
  7. import com.mongodb.MongoClient;  
  8. import com.mongodb.client.MongoCollection;  
  9. import com.mongodb.client.MongoDatabase;  
  10. import com.mongodb.client.MongoIterable;  
  11. import com.mongodb.client.model.Filters;  
  12. import com.mongodb.client.model.Projections;  
  13. import com.mongodb.client.model.Sorts;  
  14.   
  15.   
  16. public class MongoDbWithJavaTest {  
  17.   
  18.     public static void main(String[] args) {  
  19.         MongoClient mongo = null;  
  20.         try {  
  21.             // connect to mongodb  
  22.             mongo = new MongoClient("localhost"27017);  
  23.   
  24.             // list all databases  
  25.             listDatabases(mongo);  
  26.   
  27.             // get database named "test"  
  28.             MongoDatabase testDatabase = mongo.getDatabase("test");  
  29.   
  30.             // list all collections(tables)  
  31.             listCollections(testDatabase);  
  32.   
  33.             MongoCollection<Document> userCollection = testDatabase.getCollection("user");  
  34.   
  35.             // list all documents in user  
  36.             listAllDocuments(userCollection);  
  37.   
  38.             // insert new document  
  39. //            insert(userCollection);  
  40.   
  41.             // list all documents in user after insert  
  42. //            listAllDocuments(userCollection);  
  43. //            listAllSpecifiedDocumentFields(userCollection);  
  44.   
  45.             // list document with given filter  
  46. //            listDocumentWithFilter(userCollection);  
  47. //              
  48. //            listDocumentWithFilterAndInReverseOrder(userCollection);  
  49.               
  50.             // update document  
  51. //            updateOneDocument(userCollection);  
  52.             updateAllDocument(userCollection);  
  53. //              
  54. //            deleteOne(userCollection);  
  55. //            deleteMany(userCollection);  
  56. //              
  57.             listAllDocuments(userCollection);  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         } finally {  
  61.             if (mongo != null) {  
  62.                 mongo.close();  
  63.                 mongo = null;  
  64.             }  
  65.         }  
  66.     }  
  67.       
  68.     public static void deleteOne(MongoCollection<Document> collection) {  
  69.         System.out.println("delete one records age less than 24");  
  70.         collection.deleteOne(Filters.lt("age"24));  
  71.     }  
  72.       
  73.     public static void deleteMany(MongoCollection<Document> collection) {  
  74.         System.out.println("delete all records age less than 24");  
  75.         collection.deleteMany(Filters.lt("age"24));  
  76.     }  
  77.   
  78.     public static void updateOneDocument(MongoCollection<Document> collection) {  
  79.         System.out.println("updateDocument : update one records that named 'dreamoftch' to 'ZhangSan'");  
  80.         collection.updateOne(Filters.eq("name""dreamoftch"), new Document("$set"new Document("name""ZhangSan")));  
  81.     }  
  82.       
  83.     public static void updateAllDocument(MongoCollection<Document> collection) {  
  84.         System.out.println("updateDocument : update all records that named 'dreamoftch' to 'ZhangSan'");  
  85.         collection.updateMany(Filters.eq("name""dreamoftch"), new Document("$set"new Document("name""ZhangSan")));  
  86.     }  
  87.   
  88.     public static void listDatabases(MongoClient mongo) {  
  89.         // list all databases  
  90.         MongoIterable<String> allDatabases = mongo.listDatabaseNames();  
  91.         for (String db : allDatabases) {  
  92.             System.out.println("Database name: " + db);  
  93.         }  
  94.     }  
  95.   
  96.     public static void listCollections(MongoDatabase database) {  
  97.         // list all databases  
  98.         MongoIterable<String> allCollections = database.listCollectionNames();  
  99.         for (String collection : allCollections) {  
  100.             System.out.println("Collection name: " + collection);  
  101.         }  
  102.     }  
  103.       
  104.     public static void listAllDocuments(MongoCollection<Document> collection) {  
  105.         System.out.println("begin get all document >>>>>>");  
  106.         for (Document document : collection.find()) {  
  107.             System.out.println(document);  
  108.         }  
  109.         System.out.println("finish get all document >>>>>>");  
  110.     }  
  111.       
  112.     public static void listAllSpecifiedDocumentFields(MongoCollection<Document> collection) {  
  113.         System.out.println("begin get all document(exclude '_id') >>>>>>");  
  114.         for (Document document : collection.find().projection(Projections.exclude("_id"))) {  
  115.             System.out.println(document);  
  116.         }  
  117.         System.out.println("finish get all document(exclude '_id') >>>>>>");  
  118.     }  
  119.       
  120.     public static void insert(MongoCollection<Document> collection){  
  121.         List<Document> documents = new ArrayList<Document>();  
  122.         for (int i = 0; i < 10; i++) {  
  123.             documents.add(new Document("name""dreamoftch").append("age", (20+i)).append("createdDate"new Date()));  
  124.         }  
  125.         collection.insertMany(documents);  
  126.     }  
  127.       
  128.     public static void listDocumentWithFilter(MongoCollection<Document> collection) {  
  129.         System.out.println("begin get document(name: dreamoftch, age > 25) >>>>>>");  
  130.         for (Document document : collection.find(Filters.and(Filters.eq("name""dreamoftch"), Filters.gt("age"25)))) {  
  131.             System.out.println(document);  
  132.         }  
  133.         System.out.println("finish get document(name: dreamoftch, age > 25) >>>>>>");  
  134.     }  
  135.       
  136.     public static void listDocumentWithFilterAndInReverseOrder(MongoCollection<Document> collection) {  
  137.         System.out.println("begin get document(name: dreamoftch, age > 25) >>>>>>");  
  138.         for (Document document : collection.find(Filters.and(Filters.eq("name""dreamoftch"), Filters.gt("age"25))).sort(Sorts.descending("age"))) {  
  139.             System.out.println(document);  
  140.         }  
  141.         System.out.println("finish get document(name: dreamoftch, age > 25) >>>>>>");  
  142.     }  
  143. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值