MongoDB系列——官方JAVA API操作(三)

一、简介

MongoDB常用的JAVA客户端驱动有两种:

NOTE:
3.6版本的MongoDB,只允许localhost连接,如果您要使用非本地的客户端连接,需要绑定IP才可以,在这里我们关闭mongodb服务端,添加参数 --bind_ip,重新启动服务。继续测试~~~

二、maven坐标

注意:本博客演示的是官方提供的mongodb-driver

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver</artifactId>
    <version>3.6.1</version>
</dependency>
三、插入
@Test
  public void testInsert(){

      MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);

      // 获取指定数据库
      MongoDatabase database = mongoClient.getDatabase("test");

      // 获取指定集合
      MongoCollection<Document> collection = database.getCollection("users");

      // 创建需要插入的单个文档
      Document doc = new Document("name", "MongoDB")
              .append("type", "database")
              .append("count", 1)
              .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
              .append("info", new Document("x", 203).append("y", 102));

      // 测试插入
      collection.insertOne(doc);

      // 测试插入多个文档
      ArrayList<Document> documents = new ArrayList<Document>();
      for (int i = 0; i < 100; i++) {
           documents.add(new Document("i",i));
      }

      collection.insertMany(documents);
      mongoClient.close();
  }

在这里插入图片描述

四、修改
 @Test
  public void test3(){
      MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);

      // 获取指定数据库
      MongoDatabase database = mongoClient.getDatabase("test");

      // 获取指定集合
      MongoCollection<Document> collection = database.getCollection("users");

      // update t_user set i = 110 where i = 10
      //UpdateResult updateResult = collection.updateOne(new Document("i", 10), new Document("$set", new Document("i", 110)));

      // update t_user set i =10 where i < 20
      // lt(key,value)  静态导入  import static com.mongodb.client.model.Filters.lt;
      collection.updateMany(lt("i",20),new Document("$set",new Document("i",10).append("test","test value")));

      mongoClient.close();
  }

在这里插入图片描述

五、 删除
@Test
  public void test4(){
      MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);

      // 获取指定数据库
      MongoDatabase database = mongoClient.getDatabase("test");

      // 获取指定集合
      MongoCollection<Document> collection = database.getCollection("users");

      DeleteResult deleteResult = null;

      //deleteResult =collection.deleteOne(eq("i", 110));

      deleteResult = collection.deleteMany(lt("i", 20));

      System.out.println("删除的文档数:"+deleteResult.getDeletedCount());

      mongoClient.close();
  }

在这里插入图片描述

六、 查询
 @Test
  public void test5(){
      MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);

      // 获取指定数据库
      MongoDatabase database = mongoClient.getDatabase("test");

      // 获取指定集合
      MongoCollection<Document> collection = database.getCollection("users");

      // 查所有
      FindIterable<Document> documents = null;

      documents = collection.find();

      // 条件查询 select * fromt t_user where i = 20
      documents = collection.find(eq("i",20));

      // select * from t_user where i <= 20
      documents = collection.find(lte("i",30));

      // select * from t_user where i = 30 or i = 40 or i = 50
      documents = collection.find(in("i",30,40,50));

      // SELECT * FROM t_user WHERE i = 50 OR i < 25
      documents = collection.find(or(new Document("i",50),lt("i",25)));

      // 模糊查询 参数二:模糊条件
      documents = collection.find(regex("test","test"));

      // 排序 SELECT * FROM t_user WHERE i >= 90 order by i desc
      documents = collection.find(gt("i",90)).sort(Sorts.descending("i"));

      // 分页查询
      documents = collection.find().skip(50).limit(10);

      for (Document document : documents) {
          System.out.println(document);
      }

      mongoClient.close();
  }
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值