设为首页 收藏本站
查看: 806|回复: 0

[经验分享] 【MongoDB】5.MongoDB与java的简单结合

[复制链接]

尚未签到

发表于 2017-12-14 22:33:00 | 显示全部楼层 |阅读模式
package com.mongo.test;  

  

  
import java.text.SimpleDateFormat;
  
import java.util.ArrayList;
  
import java.util.List;
  

  
import org.bson.Document;
  
import org.junit.Test;
  

  
import com.mongo.util.MongoConnection;
  
import com.mongodb.client.FindIterable;
  
import com.mongodb.client.MongoCollection;
  
import com.mongodb.client.MongoCursor;
  
import com.mongodb.client.MongoDatabase;
  
import com.mongodb.client.model.Filters;
  


  
public>  
     
  
     MongoConnection connection = new MongoConnection();
  
     //连接到数据库
  
     MongoDatabase mongoDatabase = connection.getConnectionBasis();
  
     
  
     
  
     @Test
  
     public void test(){
  
         //createCollection();//创建   集合 一次就好
  
         MongoCollection<Document> collection = getCollection();
  
         insertDomcument(collection);
  
         findAll(collection);
  
         //delete(collection);
  
     }
  
     
  
     /**
  
      * 创建  集合【对应RDBMS 中的数据表】 com.mongodb.client.MongoDatabase.createCollection("集合名")
  
      */
  
     public void createCollection(){
  
         mongoDatabase.createCollection("testConllection");
  
         System.out.println("创建集合成功");
  
     }
  
     
  
     /**
  
      * 获取  集合【对应RDBMS 中的数据表】com.mongodb.client.MongoDatabase.getCollection("集合名")
  
      */
  
     public MongoCollection<Document> getCollection(){
  
         MongoCollection<Document>  collection = mongoDatabase.getCollection("testConllection");
  
         System.out.println("转换到指定集合");
  
         return collection;
  
     }
  
     
  
     /**
  
      * 插入  文档【对应RDBMS 中的一条数据】com.mongodb.client.MongoCollection<Document>.insertOne()/insertMany()
  
      */
  
     public void insertDomcument(MongoCollection<Document> collection){
  
         /**
  
         * 1. 创建文档 org.bson.Document 参数为key-value的格式
  
         * 2. 创建文档集合List<Document>
  
         * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document)
  
         * */
  
         Document document = new Document();
  
         document.append("name", "走四方");
  
         document.append("age", 23);
  
         document.append("url", "www.baidu.com");
  
         
  
         List<Document> list = new ArrayList<Document>();
  
         list.add(document);
  
         
  
         collection.insertMany(list);
  
         System.out.println("插入文档成功");
  
         
  
         //插入 单条数据
  
         Document  t = new Document();
  
         t.append("name", "走什么");
  
         t.append("age", 26);
  
         t.append("url", "www.agen.cn");
  
         
  
         collection.insertOne(t);
  
         System.out.println("插入单条数据成功");
  
     }
  
     
  
     /**
  
      * 查询  所有文档【表内 数据】com.mongodb.client.MongoCollection<Document>.find()
  
      * 查询  本条数据的时间节点   _id采用ObjectId格式
  
      *
  
      * ObjectId 是一个12字节 BSON 类型数据,有以下格式:
  
             前4个字节表示时间戳
  
             接下来的3个字节是机器标识码
  
             紧接的两个字节由进程id组成(PID)
  
             最后三个字节是随机数。
  
      */
  
     public void findAll(MongoCollection<Document> collection){
  
         /**
  
         * 1. 获取迭代器FindIterable<Document>
  
         * 2. 获取游标MongoCursor<Document>
  
         * 3. 通过游标遍历检索出的文档集合
  
         * */
  
          FindIterable<Document> findIterable = collection.find();
  
          MongoCursor<Document> mongoCursor = findIterable.iterator();
  
          while(mongoCursor.hasNext()){
  
              Document document = mongoCursor.next();
  
              System.out.println("MongoDB数据:"+document);
  
              System.out.println("本地时间:"+new SimpleDateFormat().format(document.getObjectId("_id").getDate()));
  
          }
  
     }
  
     
  
     /**
  
      * 更新   所有文档【表内  数据】com.mongodb.client.MongoCollection<Document>.updateMany()
  
      */
  
     public void update(MongoCollection<Document> collection){
  
         
  
         collection.updateMany(Filters.eq("age", 26), new Document("$set",new Document("age",100)));
  
         
  
         FindIterable<Document> findIterable = collection.find();
  
         MongoCursor<Document> cursor = findIterable.iterator();
  
         while (cursor.hasNext()) {
  
             System.out.println("更新后的MongoDB数据:"+cursor.next());
  
         }
  
     }
  
     
  
     
  
     /**
  
      * 删除  文档 com.mongodb.client.MongoCollection<Document>.deleteMany()/deleteOne()
  
      */
  
     public void delete(MongoCollection<Document> collection){
  
         // 删除符合条件的 第一个文档
  
         collection.findOneAndDelete(Filters.eq("age", 26));
  
         //删除符合条件的  所有文档
  
         collection.deleteMany(Filters.gte("age", 20));
  
         
  
         FindIterable<Document> findIterable = collection.find();
  
         MongoCursor<Document> cursor = findIterable.iterator();
  
         while(cursor.hasNext()){
  
             System.out.println("删除后的MongoDB数据:"+cursor.next());
  
         }
  
     }
  
     
  
     
  
     
  
     
  
     
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-424173-1-1.html 上篇帖子: MongoDB的启动与停止(一) 下篇帖子: mongodb研究(mongodb 内存数据库)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表