Java连接MongoDB的常用方法详解

 更新时间:2022年07月18日 16:42:59   作者:崇令  
这篇文章主要为大家详细介绍一下Java语言连接MongoDB的常用方法以及实现增删改查功能的示例代码,感兴趣的小伙伴可以跟随小编一起了解一下

一、Java链接MongoDB

1. 导入Mongo驱动包

2. 获取Mongo链接对象

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

3. 关闭链接

mc.close();

二、查看库,查看集合

1. 获取库对象

MongoDatabase db = mc.getDatabase("myschool");

2. 获取库中表的集合

MongoIterable<String> listCollectionNames = db.listCollectionNames();
        
MongoCursor<String> iterator = listCollectionNames.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

三、Java对MongoDB增删改查

1. 添加数据

a. 添加一条数据

//创建对象
Student s = new Student();
s.setSid(1);
s.setSname("王俊凯");
s.setBirthday(new Date());
s.setSsex("男");
s.setClassid(2);
 
//将数据转换为json格式
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
String json = gson.toJson(s);
 
//获取集合对象
MongoCollection<Document> collection = db.getCollection("student");
 
//添加一条数据,将json格式转换为document对象
collection.insertOne(Document.parse(json));

b. 添加多条数据

//存入数据
List<Document> dlist=new ArrayList<Document>();
 
for(int i=0; i<3; i++){
    Student s = new Student();
    s.setSid(Integer.toString(i+1));
    s.setSname("王源");
    s.setBirthday(new Date());
    s.setSsex("男");
    s.setClassid(1);
    //将数据转换为json格式
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    String json = gson.toJson(s);
    dlist.add(Document.parse(json));
}
 
//获取集合对象
MongoCollection<Document> collection = db.getCollection("student");
 
//添加多条数据
collection.insertMany(dlist);

2. 删除数据

a. 删除一条数据

//获取集合对象
MongoCollection<Document> collection = db.getCollection("student");
 
Student s = new Student();
s.setSid(1);
 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Bson bson = Document.parse(gson.toJson(s));
 
DeleteResult deleteOne = collection.deleteOne(bson);

b. 删除多条数据

//获取集合对象
MongoCollection<Document> collection = db.getCollection("student");
 
Student s = new Student();
s.setSname("王源");
 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Bson bson = Document.parse(gson.toJson(s));
 
DeleteResult deleteMany = collection.deleteMany(bson);

3. 修改数据

a. 修改一条数据

MongoCollection<Document> collection = db.getCollection("student");
 
//一个条件对象
Bson eq = Filters.eq("sname","易烊千玺");
 
//要修改的数据
Document doc = new Document();
doc.put("$set", new Document("age",22));
UpdateResult  updateone = collection.updateOne(eq, doc);
System.out.println(updateone);

b. 修改多条数据

MongoCollection<Document> collection = db.getCollection("student");
 
//多条件
Bson bson = Filters.and(Filters.gte("age", 20),Filters.lte("age", 40));
        
//要修改的数据
Document doc = new Document();        
doc.put("$set", new Document("sex","男"));
UpdateResult updateMany = collection.updateMany(bson, doc);
System.out.println(updateMany);

4. 查询数据

a. 全查

MongoCollection<Document> collection = db.getCollection("student");
 
FindIterable<Document> findAll = collection.find();
 
MongoCursor<Document> iterator = findAll.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

b. 带条件查询

MongoCollection<Document> collection = db.getCollection("student");
 
//一个条件对象
Bson eq = Filters.eq("sname","易烊千玺");
 
FindIterable<Document> findOne = collection.find(eq);
 
MongoCursor<Document> iterator = findOne.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

c. 模糊查询

MongoCollection<Document> collection = db.getCollection("student");
 
//使用正则表达式进行模糊查找
Bson eq = Filters.regex("sname","易");
 
FindIterable<Document> find = collection.find(eq);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

d. 分页查询

MongoCollection<Document> collection = db.getCollection("student");
 
//分页查询
FindIterable<Document> find = collection.find().skip(2).limit(3);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

e. 排序查询

MongoCollection<Document> collection = db.getCollection("student");
 
//排序查询  1升序   -1降序
Bson bson = new Document("sid",1);
FindIterable<Document> find = collection.find().sort(bson);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

到此这篇关于Java连接MongoDB的常用方法详解的文章就介绍到这了,更多相关Java连接MongoDB内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Mybatis中动态SQL,if,where,foreach的使用教程详解

    Mybatis中动态SQL,if,where,foreach的使用教程详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。这篇文章主要介绍了Mybatis中动态SQL,if,where,foreach的使用教程,需要的朋友可以参考下
    2017-11-11
  • Java实现PDF转图片的三种方法

    Java实现PDF转图片的三种方法

    有些时候我们需要在项目中展示PDF,所以我们可以将PDF转为图片,然后已图片的方式展示,效果很好,Java使用各种技术将pdf转换成图片格式,并且内容不失帧,本文给大家介绍了三种方法实现PDF转图片的案例,需要的朋友可以参考下
    2023-10-10
  • Java使用反射获取字段属性

    Java使用反射获取字段属性

    这篇文章主要为大家详细介绍了Java如何利用反射实现获取字段属性值,文中的示例代码讲解详细,具有很好的参考价值,希望对大家有所帮助
    2023-06-06
  • java Stream操作转换方法

    java Stream操作转换方法

    文章总结了Java 8中流(Stream) API的多种常用方法,包括创建流、过滤、遍历、分组、排序、去重、查找、匹配、转换、归约、打印日志、最大最小值、统计、连接、函数式接口等,展示了流API在处理集合数据时的强大和灵活性,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • Spring中的@Aspect注解使用详解

    Spring中的@Aspect注解使用详解

    这篇文章主要介绍了Spring中的@Aspect注解使用详解,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率,需要的朋友可以参考下
    2024-01-01
  • Java并发编程示例(三):线程中断

    Java并发编程示例(三):线程中断

    这篇文章主要介绍了Java并发编程示例(三):线程中断,在本节,我们所开发的示例程序将会创建一个线程,五秒钟后,利用中断机制强制中止这个线程,需要的朋友可以参考下
    2014-12-12
  • Java利用随机分钱模拟财富变化

    Java利用随机分钱模拟财富变化

    这篇文章主要为大家详细介绍了Java如何利用随机分钱思想模拟财富的变化,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-12-12
  • Java中IO流解析及代码实例

    Java中IO流解析及代码实例

    下面小编就为大家带来一篇关于Java中的IO流总结(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-07-07
  • Java中浅拷贝与深拷贝实例解析

    Java中浅拷贝与深拷贝实例解析

    这篇文章主要给大家介绍了关于Java中浅拷贝与深拷贝的相关资料,拷贝对象是java中经常会遇到的问题,文中通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • SpringBoot的jar包如何启动的实现

    SpringBoot的jar包如何启动的实现

    本文主要介绍了SpringBoot的jar包如何启动的实现,文中根据实例编码详细介绍的十分详尽,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论