{"id":1089,"date":"2023-03-12T11:24:42","date_gmt":"2023-03-12T03:24:42","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1089"},"modified":"2023-04-29T11:28:30","modified_gmt":"2023-04-29T03:28:30","slug":"mongodb-query-document","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-query-document\/","title":{"rendered":"MongoDB\u67e5\u8be2\u6587\u6863"},"content":{"rendered":"<h2>MongoDB \u67e5\u8be2\u6587\u6863<\/h2>\n<p>MongoDB \u67e5\u8be2\u6587\u6863\u4f7f\u7528 find() \u65b9\u6cd5\u3002<\/p>\n<p>find() \u65b9\u6cd5\u4ee5\u975e\u7ed3\u6784\u5316\u7684\u65b9\u5f0f\u6765\u663e\u793a\u6240\u6709\u6587\u6863\u3002<\/p>\n<p><!-- more --><\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>MongoDB \u67e5\u8be2\u6570\u636e\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>db.collection.find(query, projection)<\/code><\/pre>\n<ul>\n<li>query\uff1a\u53ef\u9009\uff0c\u4f7f\u7528\u67e5\u8be2\u64cd\u4f5c\u7b26\u6307\u5b9a\u67e5\u8be2\u6761\u4ef6<\/li>\n<li>projection\uff1a\u53ef\u9009\uff0c\u4f7f\u7528\u6295\u5f71\u64cd\u4f5c\u7b26\u6307\u5b9a\u8fd4\u56de\u7684\u5b57\u6bb5\u3002\u67e5\u8be2\u65f6\u8fd4\u56de\u6587\u6863\u4e2d\u6240\u6709\u952e\u503c\uff0c\u53ea\u9700\u7701\u7565\u8be5\u53c2\u6570\u5373\u53ef\uff08\u9ed8\u8ba4\u7701\u7565\uff09\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u9700\u8981\u4ee5\u6613\u8bfb\u7684\u65b9\u5f0f\u6765\u8bfb\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 pretty() \u65b9\u6cd5\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>> db.col.find().pretty()<\/code><\/pre>\n<p>pretty() \u65b9\u6cd5\u4ee5\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6765\u663e\u793a\u6240\u6709\u6587\u6863\u3002<\/p>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u9996\u5148\u63d2\u5165\u4e00\u7ec4\u96c6\u5408\u6570\u636e<\/p>\n<pre><code>> db.col.insert({title: &#039;MongoDB\u67e5\u8be2\u6587\u6863&#039;, \n    description: &#039;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;MongoDB&#039;, &#039;database&#039;, &#039;NoSQL&#039;],\n    likes: 100\n})<\/code><\/pre>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6211\u4eec\u67e5\u8be2\u96c6\u5408 col \u4e2d\u7684\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.find().pretty()\n{\n    &quot;_id&quot; : ObjectId(&quot;5aec50955c9f75852ecd9598&quot;),\n    &quot;title&quot; : &quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;MongoDB&quot;,\n        &quot;database&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 100\n}<\/code><\/pre>\n<p>\u9664\u4e86 find() \u65b9\u6cd5\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a findOne() \u65b9\u6cd5\uff0c\u5b83\u53ea\u8fd4\u56de\u4e00\u4e2a\u6587\u6863\u3002<\/p>\n<h2>MongoDB \u4e0e RDBMS Where \u8bed\u53e5\u6bd4\u8f83<\/h2>\n<p>\u5982\u679c\u4f60\u719f\u6089\u5e38\u89c4\u7684 SQL \u6570\u636e\uff0c\u901a\u8fc7\u4e0b\u8868\u53ef\u4ee5\u66f4\u597d\u7684\u7406\u89e3 MongoDB \u7684\u6761\u4ef6\u8bed\u53e5\u67e5\u8be2\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u64cd\u4f5c<\/th>\n<th style=\"text-align: center;\">\u683c\u5f0f<\/th>\n<th style=\"text-align: center;\">\u8303\u4f8b<\/th>\n<th style=\"text-align: center;\">RDBMS\u4e2d\u7684\u7c7b\u4f3c\u8bed\u53e5<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">\u7b49\u4e8e<\/td>\n<td style=\"text-align: center;\">{<key>:<value>}<\/td>\n<td style=\"text-align: center;\">db.col.find({&quot;by&quot;:&quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;}).pretty()<\/td>\n<td style=\"text-align: center;\">where by = &#8216;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#8217;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u5c0f\u4e8e<\/td>\n<td style=\"text-align: center;\">{<key>:{$lt:<value>}}<\/td>\n<td style=\"text-align: center;\">db.col.find({&quot;likes&quot;:{$lt:50}}).pretty()<\/td>\n<td style=\"text-align: center;\">where likes &lt; 50<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u5c0f\u4e8e\u6216\u7b49\u4e8e<\/td>\n<td style=\"text-align: center;\">{<key>:{$lte:<value>}}<\/td>\n<td style=\"text-align: center;\">db.col.find({&quot;likes&quot;:{$lte:50}}).pretty()<\/td>\n<td style=\"text-align: center;\">where likes &lt;= 50<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u5927\u4e8e<\/td>\n<td style=\"text-align: center;\">{<key>:{$gt:<value>}}<\/td>\n<td style=\"text-align: center;\">db.col.find({&quot;likes&quot;:{$gt:50}}).pretty()<\/td>\n<td style=\"text-align: center;\">where likes &gt; 50<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u5927\u4e8e\u6216\u7b49\u4e8e<\/td>\n<td style=\"text-align: center;\">{<key>:{$gte:<value>}}<\/td>\n<td style=\"text-align: center;\">db.col.find({&quot;likes&quot;:{$gte:50}}).pretty()<\/td>\n<td style=\"text-align: center;\">where likes &gt;= 50<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u4e0d\u7b49\u4e8e<\/td>\n<td style=\"text-align: center;\">{<key>:{$ne:<value>}}<\/td>\n<td style=\"text-align: center;\">db.col.find({&quot;likes&quot;:{$ne:50}}).pretty()<\/td>\n<td style=\"text-align: center;\">where likes != 50<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>MongoDB AND \u6761\u4ef6<\/h2>\n<p>MongoDB \u7684 find() \u65b9\u6cd5\u53ef\u4ee5\u4f20\u5165\u591a\u4e2a\u952e(key)\uff0c\u6bcf\u4e2a\u952e(key)\u4ee5\u9017\u53f7\u9694\u5f00\uff0c\u5373\u5e38\u89c4 SQL \u7684 AND \u6761\u4ef6\u3002<\/p>\n<p>\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>> db.col.find({key1:value1, key2:value2}).pretty()<\/code><\/pre>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u901a\u8fc7 by \u548c title \u952e\u7ec4\u5408\u67e5\u8be2<\/p>\n<pre><code class=\"language-javascript\">> db.col.find({&quot;by&quot;:&quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;title&quot;:&quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;}).pretty()\n{\n    &quot;_id&quot; : ObjectId(&quot;5aec50955c9f75852ecd9598&quot;),\n    &quot;title&quot; : &quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;MongoDB&quot;,\n        &quot;database&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 100\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\u7c7b\u4f3c\u4e8e WHERE \u8bed\u53e5\uff1aWHERE by=&#8217;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#8217; AND title=&#8217;MongoDB\u67e5\u8be2\u6587\u6863&#8217;<\/p>\n<h2>MongoDB OR \u6761\u4ef6<\/h2>\n<p>MongoDB OR \u6761\u4ef6\u8bed\u53e5\u4f7f\u7528\u4e86\u5173\u952e\u5b57 $or\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>> db.col.find(\n   {\n      $or: [\n         {key1: value1}, {key2:value2}\n      ]\n   }\n).pretty()<\/code><\/pre>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u6f14\u793a\u67e5\u8be2\u952e by \u503c\u4e3a APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2 \u6216\u952e title \u503c\u4e3a MongoDB\u67e5\u8be2\u6587\u6863 \u7684\u6587\u6863\u3002<\/p>\n<pre><code class=\"language-javascript\">> db.col.find({$or:[{&quot;by&quot;:&quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;}, {&quot;title&quot;: &quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;}]}).pretty()\n{\n    &quot;_id&quot; : ObjectId(&quot;5aec50955c9f75852ecd9598&quot;),\n    &quot;title&quot; : &quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;MongoDB&quot;,\n        &quot;database&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 100\n}<\/code><\/pre>\n<h2>AND \u548c OR \u8054\u5408\u4f7f\u7528<\/h2>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a AND \u548c OR \u8054\u5408\u4f7f\u7528\uff0c\u7c7b\u4f3c\u5e38\u89c4 SQL \u8bed\u53e5\u4e3a\uff1a &#8216;where likes &gt; 50 AND (by = &#8216;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#8217; OR title = &#8216;MongoDB\u67e5\u8be2\u6587\u6863&#8217;)&#8217;<\/p>\n<pre><code class=\"language-javascript\">> db.col.find({&quot;likes&quot;: {$gt:50}, $or: [{&quot;by&quot;: &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;}, {&quot;title&quot;: &quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;}]}).pretty()\n{\n    &quot;_id&quot; : ObjectId(&quot;5aec50955c9f75852ecd9598&quot;),\n    &quot;title&quot; : &quot;MongoDB\u67e5\u8be2\u6587\u6863&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;MongoDB&quot;,\n        &quot;database&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 100\n}<\/code><\/pre>\n<h2>\u8865\u5145\u4e00\u4e0b projection \u53c2\u6570\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<pre><code class=\"language-javascript\">db.collection.find(query, projection)<\/code><\/pre>\n<p>\u82e5\u4e0d\u6307\u5b9a projection\uff0c\u5219\u9ed8\u8ba4\u8fd4\u56de\u6240\u6709\u952e\uff0c\u6307\u5b9a projection \u683c\u5f0f\u5982\u4e0b\uff0c\u6709\u4e24\u79cd\u6a21\u5f0f<\/p>\n<pre><code class=\"language-javascript\">db.collection.find(query, {title: 1, by: 1})  \/\/ inclusion\u6a21\u5f0f \u6307\u5b9a\u8fd4\u56de\u7684\u952e\uff0c\u4e0d\u8fd4\u56de\u5176\u4ed6\u952e\ndb.collection.find(query, {title: 0, by: 0})  \/\/ exclusion\u6a21\u5f0f \u6307\u5b9a\u4e0d\u8fd4\u56de\u7684\u952e\uff0c\u8fd4\u56de\u5176\u4ed6\u952e<\/code><\/pre>\n<p>_id \u952e\u9ed8\u8ba4\u8fd4\u56de\uff0c\u9700\u8981\u4e3b\u52a8\u6307\u5b9a _id:0 \u624d\u4f1a\u9690\u85cf<\/p>\n<p>\u4e24\u79cd\u6a21\u5f0f\u4e0d\u53ef\u6df7\u7528\uff08\u56e0\u4e3a\u8fd9\u6837\u7684\u8bdd\u65e0\u6cd5\u63a8\u65ad\u5176\u4ed6\u952e\u662f\u5426\u5e94\u8fd4\u56de\uff09<\/p>\n<pre><code class=\"language-javascript\">db.collection.find(query, {title: 1, by: 0})  \/\/ \u9519\u8bef<\/code><\/pre>\n<p>\u53ea\u80fd\u51681\u6216\u51680\uff0c\u9664\u4e86\u5728inclusion\u6a21\u5f0f\u65f6\u53ef\u4ee5\u6307\u5b9a _id \u4e3a 0<\/p>\n<pre><code class=\"language-javascript\">db.collection.find(query, {_id:0, title: 1, by: 1})  \/\/ \u6b63\u786e<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u67e5\u8be2\u6587\u6863 MongoDB \u67e5\u8be2\u6587\u6863\u4f7f\u7528 find() \u65b9\u6cd5\u3002 find() \u65b9\u6cd5\u4ee5\u975e\u7ed3\u6784\u5316\u7684\u65b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300],"tags":[],"class_list":["post-1089","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}