{"id":3449,"date":"2018-11-29T15:50:31","date_gmt":"2018-11-29T07:50:31","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3449"},"modified":"2018-11-29T15:50:31","modified_gmt":"2018-11-29T07:50:31","slug":"%e8%af%a6%e8%a7%a3-com-mongodb-client-model-filters-%e7%9a%84%e7%94%a8%e6%b3%95%e5%92%8c%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3449","title":{"rendered":"\u8be6\u89e3 com.mongodb.client.model.Filters \u7684\u7528\u6cd5\u548c\u6559\u7a0b"},"content":{"rendered":"<p>\n\tMongoDB \u7684 Java \u64cd\u4f5c\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u7c7b\u5c31\u662f com.mongodb.client.model.Filters(\u8fc7\u6ee4\u5668)\u3002\u6211\u4eec\u7684\u5f88\u591a\u67e5\u8be2\uff0c\u6761\u4ef6\u7b5b\u9009\u90fd\u662f\u901a\u8fc7\u5b83\u5b9e\u73b0\u3002\u6bd4\u5982\uff1aeq\u3001ne\u3001gt\u3001lt\u3001gte\u3001lte\u3001in\u3001nin\u3001and\u3001or\u3001not\u3001nor\u3001exists\u3001type\u3001mod\u3001regex\u3001text\u3001where\u3001all\u3001elemMatch\u3001size\u3001bitsAllClear\u3001bitsAllSet\u3001bitsAnyClear\u3001bitsAnySet\u3001geoWithin\u3001geoWithin\u3001geoWithinBox\u3001geoWithinPolygon\u3001geoWithinCenter\u3001geoWithinCenterSphere\u3001geoIntersects\u3001near\u3001nearSphere \u7b49\u3002\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u7528\u7684\u8fc7\u6ee4\u64cd\u4f5c\u3002\n<\/p>\n<p>\n\t\u901a\u8fc7\u4e0a\u4e00\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3445\" name=\"\u8be6\u89e3 com.mongodb.client.MongoCollection \u7684\u7528\u6cd5\uff01\" target=\"_blank\" title=\"\u8be6\u89e3 com.mongodb.client.MongoCollection \u7684\u7528\u6cd5\uff01\">\u8be6\u89e3 com.mongodb.client.MongoCollection \u7684\u7528\u6cd5\uff01<\/a>\u300b\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u77e5\u9053\u3002insertMany\u3001find\u3001updateMany\u3001deleteMany \u7b49\u65b9\u6cd5\u3002\u5b83\u4eec\u7684\u8fc7\u6ee4\u6761\u4ef6\u90fd\u9700\u8981\u4e00\u4e2a org.bson.conversions.Bson \u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8981\u8fbe\u5230\u8fc7\u6ee4\u76ee\u7684\uff0c\u5fc5\u987b\u8981\u6784\u9020\u4e00\u4e2a Bson \u5bf9\u8c61\u3002Bson \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u6709\u5f88\u591a\u7684\u5b9e\u73b0\u7c7b\u3002\u6bd4\u5982\uff1aSimpleEncodingFilter \u7b49\u3002\n<\/p>\n<p>\n\t\u5176\u4e2d Filters \u8fc7\u6ee4\u5668\u7684\u4f5c\u7528\u5c31\u662f\u6784\u9020\u4e0d\u540c\u7684 Bson \u5bf9\u8c61\u3002\u8fc7\u6ee4\u6761\u4ef6\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u56e0\u6b64\u5c31\u4f1a\u6784\u6210\u4e00\u4e2a\u8fc7\u6ee4\u5668\u94fe\u3002\n<\/p>\n<p>\n\t\u5177\u4f53\u7684\u4f7f\u7528\u6559\u7a0b\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class MongoDBJDBC{\r\n   public static void main( String args[] ){\r\n      try{   \r\n         \/\/ \u8fde\u63a5\u5230 mongodb \u670d\u52a1\r\n         MongoClient mongoClient = new MongoClient( &quot;localhost&quot; , 27017 );\r\n         \r\n         \/\/ \u8fde\u63a5\u5230\u6570\u636e\u5e93\r\n         MongoDatabase mongoDatabase = mongoClient.getDatabase(&quot;xttblog&quot;);  \r\n         System.out.println(&quot;Connect to database successfully&quot;);\r\n         \r\n         MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection(&quot;test&quot;);\r\n         System.out.println(&quot;\u96c6\u5408 test \u9009\u62e9\u6210\u529f&quot;);\r\n         \r\n         \/\/\u5c06\u6587\u6863\u4e2dlikes=100\u7684\u6587\u6863\u4fee\u6539\u4e3alikes=200   \r\n         collection.updateMany(Filters.eq(&quot;likes&quot;, 100), new Document(&quot;$set&quot;,new Document(&quot;likes&quot;,200)));  \r\n\r\n         \/\/\u5c06\u6587\u6863\u4e2dlikes=100 or likes=200 \u7684\u6587\u6863\u4fee\u6539\u4e3alikes=300 \r\n         collection.updateMany(Filters.or(Filters.eq(&quot;likes&quot;, 100), Filters.eq(&quot;likes&quot;, 300)), new Document(&quot;$set&quot;,new Document(&quot;likes&quot;,300)));  \r\n\r\n         \/\/\u5c06\u6587\u6863\u4e2dlikes=300 AND _id=200 \u7684\u6587\u6863\u4fee\u6539\u4e3alikes=400 \r\n         collection.updateMany(Filters.and(Filters.eq(&quot;likes&quot;, 100), Filters.eq(&quot;id&quot;, 200)), new Document(&quot;$set&quot;,new Document(&quot;likes&quot;,400))); \r\n\r\n         \/\/\u5c06\u6587\u6863\u4e2dlikes != 100 \u7684\u6587\u6863\u4fee\u6539\u4e3alikes=500 \r\n         collection.updateMany(Filters.ne(&quot;likes&quot;, 100)), new Document(&quot;$set&quot;,new Document(&quot;likes&quot;,500))); \r\n\r\n         \/\/\u5c06\u6587\u6863\u4e2dlikes &gt; 100 \u7684\u6587\u6863\u4fee\u6539\u4e3alikes=600 \r\n         collection.updateMany(Filters.gt(&quot;likes&quot;, 100)), new Document(&quot;$set&quot;,new Document(&quot;likes&quot;,600))); \r\n\r\n         \/\/\u5c06\u6587\u6863\u4e2dlikes &lt; 800 \u7684\u6587\u6863\u4fee\u6539\u4e3alikes=700 \r\n         collection.updateMany(Filters.lt(&quot;likes&quot;, 100)), new Document(&quot;$set&quot;,new Document(&quot;likes&quot;,700))); \r\n         \/\/ ......\r\n         \/\/\u68c0\u7d22\u67e5\u770b\u7ed3\u679c  \r\n         FindIterable&lt;Document&gt; findIterable = collection.find();  \r\n         MongoCursor&lt;Document&gt; mongoCursor = findIterable.iterator();  \r\n         while(mongoCursor.hasNext()){  \r\n            System.out.println(mongoCursor.next());  \r\n         }  \r\n      \r\n      }catch(Exception e){\r\n         System.err.println( e.getClass().getName() + &quot;: &quot; + e.getMessage() );\r\n      }\r\n   }\r\n}<\/pre>\n<p>\n\t\u5e38\u7528\u7684\u51e0\u4e2a\u6bd4\u8f83\uff0c\u6211\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u4e00\u4e2a\u8868\u683c\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"MongoDB \u7684 Java \u68c0\u7d22\u6761\u4ef6\u7528\u6cd5\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/mongodb-where-rdbms.png\" style=\"width: 839px;height: 292px\" \/>\n<\/p>\n<p>\n\t\u50cf\u6211\u524d\u9762\u4ecb\u7ecd\u7684\u8fd9\u4e9b eq\u3001ne\u3001gt\u3001lt\u3001gte\u3001lte\u3001in\u3001nin\u3001and\u3001or\u3001not\u3001nor\u3001exists\u3001type\u3001mod\u3001regex\u3001text\u3001where\u3001all\u3001elemMatch\u3001size\u3001bitsAllClear\u3001bitsAllSet\u3001bitsAnyClear\u3001bitsAnySet\u3001geoWithin\u3001geoWithin\u3001geoWithinBox\u3001geoWithinPolygon\u3001geoWithinCenter\u3001geoWithinCenterSphere\u3001geoIntersects\u3001near\u3001nearSphere \u7528\u6cd5\u90fd\u5f88\u7c7b\u4f3c\u3002\u5927\u5bb6\u81ea\u5df1\u53bb\u52a8\u624b\u5199\u4e2a demo \u5c31 OK \u4e86\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3449\">\u8be6\u89e3 com.mongodb.client.model.Filters \u7684\u7528\u6cd5\u548c\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>MongoDB \u7684 Java \u64cd\u4f5c\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u7c7b\u5c31\u662f com.mongodb.client.model.F [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12],"tags":[1767,1578,1577,747,1402],"class_list":["post-3449","post","type-post","status-publish","format-standard","hentry","category-java","category-sql","tag-mongocollection","tag-mongodb","tag-nosql","tag-747","tag-1402"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3449"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3449\/revisions"}],"predecessor-version":[{"id":3450,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3449\/revisions\/3450"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}