Java基础、中级、高级、架构面试资料

详解 com.mongodb.client.model.Filters 的用法和教程

JAVA herman 8385浏览
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

MongoDB 的 Java 操作中,最重要的一个类就是 com.mongodb.client.model.Filters(过滤器)。我们的很多查询,条件筛选都是通过它实现。比如:eq、ne、gt、lt、gte、lte、in、nin、and、or、not、nor、exists、type、mod、regex、text、where、all、elemMatch、size、bitsAllClear、bitsAllSet、bitsAnyClear、bitsAnySet、geoWithin、geoWithin、geoWithinBox、geoWithinPolygon、geoWithinCenter、geoWithinCenterSphere、geoIntersects、near、nearSphere 等。本文主要介绍几个常用的过滤操作。

通过上一章《详解 com.mongodb.client.MongoCollection 的用法!》的学习,我们知道。insertMany、find、updateMany、deleteMany 等方法。它们的过滤条件都需要一个 org.bson.conversions.Bson 对象。因此,我们要达到过滤目的,必须要构造一个 Bson 对象。Bson 是一个接口,它有很多的实现类。比如:SimpleEncodingFilter 等。

其中 Filters 过滤器的作用就是构造不同的 Bson 对象。过滤条件可以有多个,因此就会构成一个过滤器链。

具体的使用教程如下:

public class MongoDBJDBC{
   public static void main( String args[] ){
      try{   
         // 连接到 mongodb 服务
         MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
         
         // 连接到数据库
         MongoDatabase mongoDatabase = mongoClient.getDatabase("xttblog");  
         System.out.println("Connect to database successfully");
         
         MongoCollection<Document> collection = mongoDatabase.getCollection("test");
         System.out.println("集合 test 选择成功");
         
         //将文档中likes=100的文档修改为likes=200   
         collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200)));  

         //将文档中likes=100 or likes=200 的文档修改为likes=300 
         collection.updateMany(Filters.or(Filters.eq("likes", 100), Filters.eq("likes", 300)), new Document("$set",new Document("likes",300)));  

         //将文档中likes=300 AND _id=200 的文档修改为likes=400 
         collection.updateMany(Filters.and(Filters.eq("likes", 100), Filters.eq("id", 200)), new Document("$set",new Document("likes",400))); 

         //将文档中likes != 100 的文档修改为likes=500 
         collection.updateMany(Filters.ne("likes", 100)), new Document("$set",new Document("likes",500))); 

         //将文档中likes > 100 的文档修改为likes=600 
         collection.updateMany(Filters.gt("likes", 100)), new Document("$set",new Document("likes",600))); 

         //将文档中likes < 800 的文档修改为likes=700 
         collection.updateMany(Filters.lt("likes", 100)), new Document("$set",new Document("likes",700))); 
         // ......
         //检索查看结果  
         FindIterable<Document> findIterable = collection.find();  
         MongoCursor<Document> mongoCursor = findIterable.iterator();  
         while(mongoCursor.hasNext()){  
            System.out.println(mongoCursor.next());  
         }  
      
      }catch(Exception e){
         System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      }
   }
}

常用的几个比较,我给大家整理了一个表格,如下图所示:

MongoDB 的 Java 检索条件用法

像我前面介绍的这些 eq、ne、gt、lt、gte、lte、in、nin、and、or、not、nor、exists、type、mod、regex、text、where、all、elemMatch、size、bitsAllClear、bitsAllSet、bitsAnyClear、bitsAnySet、geoWithin、geoWithin、geoWithinBox、geoWithinPolygon、geoWithinCenter、geoWithinCenterSphere、geoIntersects、near、nearSphere 用法都很类似。大家自己去动手写个 demo 就 OK 了。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » 详解 com.mongodb.client.model.Filters 的用法和教程