Java操作MongoDB数据库(连接,增删改查)

连接背景:需使用第三方类库:mongo-java-driver-3.4.2.jar

不同的MongoDB需要使用的版本jar不同,根据需要可以自己在官网下载适合自己的版本

gson-2.8.8.jar:某些方法需要传入MongoDB所需的Bson对象,可通过Gson,fastjson等方式创建,此处用的Gson

1.连接MongoDB数据库:

返回值:一个数据库连接对象

参数: 参数一:String类型的ip,本地数据库,可写localhost 或者127.0.0.1

参数二:mongoDB的端口号,一般都是27017,根据自己安装端口号填写
MongoClient mc = new MongoClient("localhost",27017);

2.获取所有的数据库:通过连接对象的listDataBaseNames()方法获取
返回值:一个数据库名的集合,String类型,包含三个基本库admin,config,local
还有自己创建的所有数据库
MongoIterabble<String> dblist = mc.listDataBaseNames();
3.获取指定数据库:通过连接对象的getDatabase()获取,
返回值:一个具体的数据库对象
MongoDatabase db = mc.getDatabase("数据库名");
4.得到所有集合:

       注意:MongoDB中的集合类似MYSQL中的表,表中的每条数据都是一个文档(Document对象)

返回值:一个Docement类型的文档集合
ListCollectionsIterable<Document> colllist = db.listCollections();
5.得到指定的数据库集合
返回值:一个具体的数据库集合(MongoDB中的表)
MongoCollection<Document> coll = db.getCollection("集合名");
6.释放资源

注意:作为连接对象,使用完必须关闭,否则浪费资源

mc.close()
7.java操作mongoDB添加数据:

首先,我们先要 建立连接,获取到所使用的数据库以及集合(MongoDB中的集合,类似表)

MongoDB的连接驱动为我们提供了insertOne和insertmany方法,可一次添加一条或者多条数据

但是方法内部必须是一个Document的对象,或者Document对象组成的集合

        MongoClient mc = new MongoClient("localhost",27017);
        MongoDatabase db  = mc.getDatabase("myschool");
        MongoCollection<Document> teacher = db.getCollection("teacher");
        //添加一条数据
//        Document doc = new Document();
//        doc.append("tname", "老刘");
//        doc.append("tsex", "男");
//        doc.append("tbir", new Date());
//        doc.append("tage", 40);
//        teacher.insertOne(doc);
        //添加多条数据
        Document doc1 = new Document();
        doc1.append("tname", "老杨");
        doc1.append("tbir", "1992-2-2");
        doc1.append("tage", 28);
        
        Document doc2 = new Document();
        doc2.append("tname", "老王");
        doc2.append("tsex", "男");
        doc2.append("tbir", "1991-2-3");
        doc2.append("tage", 32);
        
        Document doc3 = new Document();
        doc3.append("tname", "小彭");
        doc3.append("tsex", "男");
        doc3.append("tbir", "2002-2-3");
        doc3.append("tage", 22);
        
        List<Document> dolist = Arrays.asList(doc1,doc2,doc3);
        teacher.insertMany(dolist);
        System.out.println("添加成功");
        mc.close();

8.java操作MongoDB删除数据:

同样的,我们先获取连接,使用数据库,以及集合对象,以及Document对象,将删除数据的条件添加至doc对象中,在调用方法deleteOne或者deleteMany删除

注意:无论删除多条还是单条数据,都有返回值,DeleteResult对象,

通过调用getDeletedCount()可得到删除记录的行数

        MongoClient mc = new MongoClient("localhost",27017);
        MongoDatabase db = mc.getDatabase("myschool");
        MongoCollection<Document> coll = db.getCollection("teacher");
        Document b1 = new Document();
        b1.append("tname", "老王");

        //只会删除编号最小的
        DeleteResult deleteOne = coll.deleteOne(b1);
        long count = deleteOne.getDeletedCount();
        System.out.println(count);
        //删除多条数据
        DeleteResult deleteMany = coll.deleteMany(b1);
        long count = deleteMany.getDeletedCount();
        System.out.println(count);
        mc.close();

9.java操作MongoDB修改数据:

同样的,我们先获取连接,使用数据库,以及集合对象,以及Document对象,将删除数据的条件添加至doc对象中,在调用方法updateMany方法修改

此方法需要两个参数:

参数一:Bson对象,利用GSON的Filter类,创建,作为筛选数据的条件

参数二:作为要修改的值

注意:此方法返回的updateResult对象,可获取多个结果

matchedCount:匹配到数据的行数

modifiedCount:修改的数量;

包括没匹配到,但是直接添加的方法。详情参考我的贴子MongoDB学习总结

                    MongoClient mc = new MongoClient("localhost",27017);
                MongoDatabase db = mc.getDatabase("myschool");
                MongoCollection<Document> coll = db.getCollection("teacher");
                //修改单条数据
                Bson b1 = Filters.eq("tname","老刘");
                Document doc = new Document();
                doc.append("$set", new Document("tage",51));

                UpdateResult updateOne = coll.updateMany(b1, doc);
                long matchedCount = updateOne.getMatchedCount();
                long modifiedCount = updateOne.getModifiedCount();
                System.out.println(matchedCount);
                System.out.println(modifiedCount);

9.java操作MongoDB查询数据:

此外我们需要利用GSON可以将一些内容直接变为需要的Bson对象。

MongoClient mc = new MongoClient("localhost",27017);
        MongoDatabase db = mc.getDatabase("myschool");
        MongoCollection<Document> coll = db.getCollection("teacher");
        //条件
        //Bson b1 = Filters.eq("tname","老杨");
        //模糊查询
        //以什么开头 ^
        //以什么结尾  $
        Bson b1 = Filters.regex("tname","老");
        //再加分页
//        FindIterable<Document> find = coll.find(b1).skip((2-1)*3).limit(3);
        
        FindIterable<Document> find = coll.find().sort(new Document("tage",1));
        for(Document doc : find) {
            System.out.println(doc);
        }
        mc.close();

此处,我们学习到此,另外还包含了orm关系映射的学习,等等,再次不一一介绍

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cph_507

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值