MongoDB是我们常用的非关系型数据库之一,今天我们了解一下如何使用Java去连接使用MongoDB。

1.导入MongoDB驱动包

2.获取Mongo连接对象

MongoClient mc = new MongoClient("localhost",27017);

3.查看库、集合(表)

//从链接对象中拿到库对象
MongoDatabase database = mc.getDatabase("库名");
//从库对象中拿到某个集合(表)
MongoCollection<Document> collection = database.getCollection("表名")

4.查看集合中的数据

//调用find方法,拿到表中的数据,是一个二进制格式的文档
FindIterable<Document> find = collection.find();//将拿到的文档进行迭代器迭代,拿到迭代器对象
MongoCursor<Document> iterator = find.iterator();//hasNext进行判断是否有下一条
while(iterator.hasNext()) {Document next = iterator.next();System.out.println(next);
}

5.关闭连接

mc.close();

在Java中进行MongoDB的增删改查

首先我们需要拿到库对象和集合对象,接下来我们的库对象以myschool为例,集合对象以student为例

1.增加数据

public static void main(String[] args) {//链接对象MongoClient mgc=new MongoClient("localhost",27017);//库对象MongoDatabase bbsDB = mgc.getDatabase("myschool");//表对象MongoCollection<Document> comm = bbsDB.getCollection("teacher");     Student s= new Student();s.setBirthday(new Date());s.setClassid(100);s.setSid(3);s.setSname("猪猪");s.setSsex("女");Student s1= new Student();s.setBirthday(new Date());s.setClassid(100);s.setSid(5);s.setSname("猪猪侠");s.setSsex("男");Document doc = new Document();doc.put("sid", s.getSid());doc.put("classid", s.getClassid());doc.put("birthday", s.getBirthday());doc.put("sname", s.getSname());doc.put("ssex", s.getSsex());//一条数据comm.insertOne(doc);//多条数据//    comm.insertMany(List<document>);mgc.close();}

2.删除数据

        //Filter 通过提供的条件发布方法得到Bson对象Bson bson=Filters.eq("sid", 2);//获取链接对象MongoClient mc =new MongoClient("localhost",27017);MongoDatabase database = mc.getDatabase("kuku");MongoCollection<Document> collection = database.getCollection("Student");//删除 有返回值DeleteResult deleteOne = collection.deleteOne(bson);System.out.println(deleteOne.getDeletedCount());if(deleteOne.getDeletedCount()>0) {System.out.println("删除成功");}else{System.out.println("删除失败");}mc.close();

3.修改数据

//链接对象MongoClient mgc=new MongoClient("localhost",27017);//库对象MongoDatabase bbsDB = mgc.getDatabase("myschool");//表对象MongoCollection<Document> comm = bbsDB.getCollection("teacher");//条件Bson eq = Filters.and(Filters.gt("age", 5),Filters.lt("age", 63));//修改的数据Document doc = new Document();doc.put("$set", new Document("ssex","女"));//修改UpdateResult many = comm.updateMany(eq, doc);System.out.println(many);if(many.getModifiedCount()>0) {System.out.println("chenggong");}else {System.out.println("shibai");}mgc.close();}

4.查找数据

//链接对象MongoClient mgc=new MongoClient("localhost",27017);//库对象MongoDatabase bbsDB = mgc.getDatabase("myschool");//表对象MongoCollection<Document> comm = bbsDB.getCollection("teacher");     FindIterable<Document> find = comm.find();//迭代器对象MongoCursor<Document> iterator = find.iterator();List<Student> slist= new ArrayList<>();Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();while(iterator.hasNext()) {Document next = iterator.next();//变成jsonString json = next.toJson();//gson 转换Student s = gson.fromJson(json, Student.class);//存入集合slist.add(s);}mgc.close();

Java语言连接MongoDB常用的方法相关推荐

  1. Java语言中的常用包、访问控制

    Java常用的语言包 Java的核心类都放在Java包以及其子包下,Java扩展的许多类都放在Javax包以及其子包下.这些实用类也就是前面所说的API(应用程序接口),Oracle按这些类的功能分别 ...

  2. 脑科学核磁共振——功能连接的常用度量方法

    如图1所示,介绍一种功能连接常用度量的分类法,并简要描述每种方法的主要目的. 图1. 功能连接测量的常用方法分类 脑科学核磁共振--功能连接的常用度量方法 计算时是否规定了功能连接的方向 Pearso ...

  3. IDEA中使用Java语言连接MySQL,实现增、删、查操作

    IDEA中使用Java语言连接MySQL,实现增.删.查操作 连接条件 想要连接MySQL数据库首先需要一个mysql-connector-java-8.0.25.jar包 [官网下载](MySQL ...

  4. 程序员的开发工具:Java语言开发人员常用软件

    我是 ABin-阿斌:写一生代码,创一世佳话,筑一览芳华. 如果小伙伴们觉得我的文章有点 feel ,那就点个赞再走哦. 文章目录 一.前言 二.相关软件介绍: 1. IDEA(IntelliJ ID ...

  5. Java中连接字符串的最佳方法

    最近有人问我这个问题–在Java中使用+运算符连接字符串是否对性能不利? 这让我开始思考Java中连接字符串的不同方法,以及它们如何相互对抗. 这些是我要研究的方法: 使用+运算符 使用StringB ...

  6. java语言复制数组的四种方法

    来源于牛客网的一道选择题: Java语言的下面几种数组复制方法中,哪个效率最高? A.for循环逐一复制 B.System.arraycopy C.System.copyof D.使用clone方法 ...

  7. Java语言概述及常用DOS命令

    java基础知识图解 常用的DOS命令 使用案例

  8. java http连接_Java中通过方法创建一个http连接并请求(服务器间进行通信)

    服务器间进行通信只能通过流(Stream)的方式进行,不能用方法的返回值. 1.Java代码创建一个连接并请求该连接返回的数据 doGet()方法,execute()方法中调用 package dem ...

  9. java JDBC连接Oracle数据库的方法

    1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 4 private static final String URI = ...

最新文章

  1. Blender与Substance painter制作三维手枪
  2. 【.Net MF网络开发板研究-01】IP地址设定及简单web演示
  3. CentOS 初体验六:登录工具PuTTY使用
  4. [vue-router] Duplicate named routes definition
  5. 博文视点官方blog正式开张~~~~~~~~~
  6. 火山PC后台操作第三方窗口案例
  7. 淘宝帝国是如何创建的连载04
  8. win10硬盘锁怎么解除_win10如何使用bitlocker解锁硬盘加密
  9. PyG快速安装(一键脚本,2021.7.14简单有效)
  10. 树莓派外设开发——IIC接口OLED屏幕
  11. LetAllLinesOfCodeSpeak_杂记
  12. 12.1 hashlib--安全的哈希计算和签名库
  13. 智能优化算法:侏儒猫鼬优化算法-附代码
  14. 达梦企业管理器DEM的安装部署
  15. M301H,M301A,CM201系列盒子刷机
  16. outer和left outer join有什么区别??
  17. UNR 1 DAY 2 T1 Jakarta Skyscrapers
  18. 智能公交系统电子站牌设计
  19. Ranch基本结构图
  20. 观测天体物理学 第一章 天体信息

热门文章

  1. C++非递归算法遍历二叉链表
  2. Java常见的十种“运行时异常”
  3. 华为OD机试 - 单词倒序 | 备考思路,刷题要点,答疑 【新解法】
  4. GITHUB删除历史记录
  5. python中循环语句只有for和while两种_Python循环语句之while,for语句详解
  6. tensorflow学习(二)——finetune预训练网络--以mobileNetV1为例
  7. 如何查看linux系统是Centos还是ubuntu
  8. 航班起降动态api 航班起降时间查询代码
  9. 【转帖】利用wsdl4j解析WSDL文件
  10. 验证码(easy-captcha)