{"id":3477,"date":"2018-12-04T15:19:43","date_gmt":"2018-12-04T07:19:43","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3477"},"modified":"2018-12-04T15:19:43","modified_gmt":"2018-12-04T07:19:43","slug":"%e8%af%a6%e8%a7%a3-com-mongodb-client-model-projections-%e9%99%90%e5%88%b6-mongodb-%e6%9f%a5%e8%af%a2%e8%bf%94%e5%9b%9e%e5%ad%97%e6%ae%b5%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3477","title":{"rendered":"\u8be6\u89e3 com.mongodb.client.model.Projections \u9650\u5236 MongoDB \u67e5\u8be2\u8fd4\u56de\u5b57\u6bb5\u7528\u6cd5"},"content":{"rendered":"<p>\n\tProjections \u662f MongoDB Java \u9a71\u52a8\u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\u3002\u5b83\u91cc\u9762\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u9759\u6001\u65b9\u6cd5\uff0cProjections \u662f\u4e00\u4e2a final \u7c7b\uff0c\u5e76\u4e14\u5b83\u4e0d\u53ef\u4ee5\u88ab\u5b9e\u4f8b\u5316\u3002\u6709\u4eba\u628a\u5b83\u7ffb\u8bd1\u6210&ldquo;\u6295\u5f71&rdquo;\uff0c\u5176\u5b9e\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u4e3a\u9884\u671f\uff0c\u9884\u6d4b\u3002\u672c\u6587\u4ecb\u7ecd\u5b83\u91cc\u9762\u7684\u4e00\u4e9b\u9759\u6001\u65b9\u6cd5\u7684\u76f8\u5173\u7528\u6cd5\u3002\n<\/p>\n<p>\n\tcom.mongodb.client.model.Projections \u7c7b\u7684\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u8fd4\u56de Bson \u7c7b\u578b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u9ed8\u8ba4\u7684\u67e5\u8be2\u4f1a\u8fd4\u56de\u6bcf\u4e2a\u6587\u6863\u7684\u6240\u6709\u5b57\u6bb5\uff0c\u5982\u679c\u9700\u8981\u60f3 SQL \u67e5\u8be2\u90a3\u6837\uff0c\u8fd4\u56de\u53ea\u9700\u8981\u7684\u5b57\u6bb5\u8be5\u600e\u4e48\u529e\u5462\uff1f\u7b54\u6848\u5c31\u662f Projections.include() \u65b9\u6cd5\u3002\u88ab include \u5305\u542b\u7684\u5b57\u6bb5\u4f1a\u548c(\u9690\u5f0f\u7684)_id \u5b57\u6bb5\u4e00\u8d77\u8fd4\u56de\u3002\n<\/p>\n<p>\n\t\u5982\u4e0b\u9762\u7684\u7528\u6cd5\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\ninclude(&quot;xttblog&quot;, &quot;name&quot;)<\/pre>\n<p>\n\t\u5982\u679c\u9700\u8981\u5305\u542b\u7684\u5b57\u6bb5\u592a\u591a\uff0c\u53ef\u4ee5\u4f7f\u7528 Projection.exclude \u65b9\u6cd5\uff0c\u53cd\u5411\u6392\u9664\u4e00\u4e9b\u5b57\u6bb5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nexclude(&quot;xttblog&quot;, &quot;url&quot;)<\/pre>\n<p>\n\texclude \u548c include \u65b9\u6cd5\u90fd\u4f1a\u5305\u542b _id \u5b57\u6bb5\uff0c\u4e5f\u5c31\u662f ObjectId \u90fd\u4f1a\u67e5\u8be2\u51fa\u6765\u3002\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u6392\u9664 ObjectId \u5462\uff1f\u7b54\u6848\u5c31\u662f excludeId \u65b9\u6cd5\u3002excludeId() \u65b9\u6cd5\uff0c\u76f8\u5f53\u4e8e exclude(&quot;_id&quot;)\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u53ea\u8981\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u7b2c\u4e00\u6761\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528 elemMatch \u65b9\u6cd5\u3002\u5982\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nelemMatch(&quot;orders&quot;, Filters.gt(&quot;quantity&quot;, 3))<\/pre>\n<p>\n\t\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u53ea\u4f1a\u8fd4\u56de orders \u6587\u6863\u7684 quantity \u5b57\u6bb5\u5927\u4e8e 3 \u7684\u7b2c\u4e00\u4e2a\u7ed3\u679c\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u67d0\u4e2a\u5b57\u6bb5\u5bf9\u5e94\u7684\u503c\u662f\u4e00\u4e2a\u6570\u7ec4\u3002\u9700\u8981\u5bf9\u8fd9\u4e2a\u6570\u7ec4\u8fdb\u884c\u5206\u5272\u53d6\u503c\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528 slice \u65b9\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nslice(&quot;tags&quot;, 7) \/\/\u8fd4\u56de tag \u6570\u7ec4\u7684\u524d 7 \u4e2a\u5143\u7d20\r\nslice(&quot;tags&quot;, 2, 5) \/\/\u8df3\u8fc7\u6570\u7ec4 tags \u7684\u524d 2 \u4e2a\u5143\u7d20, \u8fd4\u56de\u63a5\u4e0b\u6765\u7684 5 \u4e2a\u5143\u7d20<\/pre>\n<p>\n\tMongoDB \u652f\u6301\u5bf9\u6587\u672c\u5185\u5bb9\u6267\u884c\u6587\u672c\u641c\u7d22\u64cd\u4f5c\uff0c\u5176\u63d0\u4f9b\u4e86\u7d22\u5f15text index\u548c\u67e5\u8be2\u64cd\u4f5c $text \u6765\u5b8c\u6210\u6587\u672c\u641c\u7d22\u529f\u80fd\u3002Projections \u4e2d\u7684 metaTextScore \u65b9\u6cd5\u63d0\u4f9b\u5bf9\u6587\u672c\u7d22\u5f15\u5339\u914d\u5230\u7684\u6570\u636e\u6253\u5206\u3002\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\ndb.articles.find(\r\n&nbsp; &nbsp;{ author: &quot;xyz&quot;, $text: { $search: &quot;coffee bake&quot; } },\r\n&nbsp; &nbsp;{ score: { $meta: &quot;textScore&quot; } }\r\n).sort( { date: 1, score: { $meta: &quot;textScore&quot; } } )<\/pre>\n<p>\n\t\u4e0a\u9762\u7684\u67e5\u8be2\u4f1a\u641c\u7d22 author \u7b49\u4e8e equals &quot;xyz&quot;\u548c\u7d22\u5f15\u6587\u672c\u5b57\u6bb5\u5305\u542b\u672f\u8bed coffee \u6216 bake \u7684\u6587\u6863 \u3002\u8be5\u64cd\u4f5c\u8fd8\u6307\u5b9a\u4e86\u6309 date \u5347\u5e8f\u6392\u5217\uff0c\u7136\u540e\u964d\u5e8f\u6587\u672c\u641c\u7d22\u5206\u6570\u7684\u6392\u5e8f\u987a\u5e8f\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u8981\u7ec4\u5408\u591a\u4e2a\u8fd4\u56de\u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528 fields \u65b9\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nfields(include(&quot;quantity&quot;, &quot;totalAmonut&quot;), excludeId())<\/pre>\n<p>\n\t\u53e6\u5916\u8fd8\u6709\u4e00\u4e2a\u4e0d\u5e38\u7528\u7684\u65b9\u6cd5 computed\uff0c\u5b83\u76f8\u5f53\u4e8e $project \u7684\u7528\u6cd5\u3002\u5177\u4f53\u5927\u5bb6\u53ef\u4ee5\u81ea\u5df1\u53bb\u6478\u7d22\u4e00\u4e0b\uff01<\/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=3477\">\u8be6\u89e3 com.mongodb.client.model.Projections \u9650\u5236 MongoDB \u67e5\u8be2\u8fd4\u56de\u5b57\u6bb5\u7528\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Projections \u662f MongoDB Java \u9a71\u52a8\u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\u3002\u5b83\u91cc\u9762\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u9759\u6001\u65b9\u6cd5\uff0cProj [&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],"tags":[1776,1774,1578,1775,1773],"class_list":["post-3477","post","type-post","status-publish","format-standard","hentry","category-java","tag-basicdbobject","tag-mongoclient","tag-mongodb","tag-mongoexception","tag-projections"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3477","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=3477"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3477\/revisions"}],"predecessor-version":[{"id":3478,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3477\/revisions\/3478"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}