MongoDB学习之Java操作MongoDB MongoDB Java Driver2.X

MongoDB学习之Java操作MongoDB   MongoDB Java Driver2.X

首先,我们的先开启mongodb服务,以及创建需要连接的数据库,以及连接的用户。

创建数据库:https://blog.csdn.net/lwx356481/article/details/82215723

创建用户:https://blog.csdn.net/lwx356481/article/details/82461539

准备好了这些之后,可以下载 MongoDB Java Driver的jar包,本次使用的是2.14.3版本的jar包

使用idea创建一个java的工程。然后开始编写工程。

首先我们来写连接数据库mongo的demo

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            for (String name : db.getCollectionNames()) {
                System.out.println("集合名称:" + name);
            }
            db.getCollection("deptno").drop();
        }
        client.close();

连接上数据库之后,进行插入数据的操作的demo

        MongoClientURI uri = new MongoClientURI("mongodb://root:root@localhost:27001/mldn");
        //获取连接mongodb数据库
        MongoClient client = new MongoClient(uri);
        //连接mldn的数据库
        MongoDatabase db = client.getDatabase("mldn");
        MongoCollection col = db.getCollection("stucol");
        List<Document> all = new ArrayList<Document>();
        for (int i = 0; i < 1000; i++) {
            Document doc = new Document();
            doc.append("sid",i);
            doc.append("name","姓名 - " + i);
            doc.append("sex",i%2==0?"M":"F");
            all.add(doc);
        }
        col.insertMany(all);
        client.close();

插入完成数据之后,进行数据的基本查询

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            DBCursor cursor = dbCollection.find();
            while (cursor.hasNext()){
                System.out.println(cursor.next());
            }
        }
        client.close();

范围查询

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库集合
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            DBObject dbObject = new BasicDBObject(); //设置过滤的条件
            dbObject.put("deptno",new BasicDBObject("$in",new int[]{1,2,3,4,5,6,7,8,9}));
            //跳过10条,显示10条
            DBCursor cursor = dbCollection.find(dbObject).skip(1).limit(100);
            while (cursor.hasNext()){
                System.out.println(cursor.next());
            }
        }
        client.close();

分页查询

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库集合
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            //跳过10条,显示10条
            DBCursor cursor = dbCollection.find().skip(10).limit(10);
            while (cursor.hasNext()){
                System.out.println(cursor.next());
            }
        }
        client.close();

模糊查询(查询的字段为str)

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库集合
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            //设置正则表达式
            Pattern pattern = Pattern.compile("5");
            DBObject dbObject = new BasicDBObject(); //设置过滤的条件
            dbObject.put("deptname",new BasicDBObject("$regex",pattern));
            //跳过10条,显示10条
            DBCursor cursor = dbCollection.find(dbObject);
            while (cursor.hasNext()){
                System.out.println(cursor.next());
            }
        }
        client.close();

条件查询

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库集合
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            DBObject dbObject = new BasicDBObject(); //设置过滤的条件
            dbObject.put("deptno",new BasicDBObject("$gte",0).append("$lte",50));
            //跳过10条,显示10条
            DBCursor cursor = dbCollection.find(dbObject).skip(10).limit(100);
            while (cursor.hasNext()){
                System.out.println(cursor.next());
            }
        }
        client.close();

更新操作

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库集合
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            DBObject dbObject = new BasicDBObject();
            dbObject.put("deptno",new BasicDBObject("$gte",0).append("$lte",5));
            DBObject dbObject2 = new BasicDBObject();
            dbObject2.put("$set",new BasicDBObject("deptname","test"));
            WriteResult result = dbCollection.updateMulti(dbObject,dbObject2);
            System.out.println(result.getN());
        }
        client.close();

删除操作

        //获取连接mongodb数据库
        MongoClient client = new MongoClient("localhost",27001);
        //连接mldn的数据库集合
        DB db = client.getDB("mldn");
        //进行授权登录
        if(db.authenticate("root","root".toCharArray())){
            //连接deptcol的集合
            DBCollection dbCollection = db.getCollection("deptcol");
            DBObject dbObject = new BasicDBObject();
            dbObject.put("deptno",new BasicDBObject("$gte",0).append("$lte",5));
            WriteResult result = dbCollection.remove(dbObject);
            System.out.println(result.getN());
        }
        client.close();

版权声明: 原创文章,如需转载,请注明出处。 https://blog.csdn.net/lwx356481/article/details/82492111

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值