{"id":1087,"date":"2023-03-12T11:09:31","date_gmt":"2023-03-12T03:09:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1087"},"modified":"2023-04-29T11:28:45","modified_gmt":"2023-04-29T03:28:45","slug":"mongodb-collection-basic-operations","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-collection-basic-operations\/","title":{"rendered":"MongoDB\u96c6\u5408\u57fa\u672c\u64cd\u4f5c"},"content":{"rendered":"<h2>MongoDB\u521b\u5efa\u96c6\u5408<\/h2>\n<h3>\u521b\u5efa\u96c6\u5408\u8bed\u6cd5<\/h3>\n<p>MongoDB \u4e2d\u4f7f\u7528 createCollection() \u65b9\u6cd5\u6765\u521b\u5efa\u96c6\u5408\u3002<\/p>\n<p>\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n<pre><code>db.createCollection(name, options)<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>name: \u8981\u521b\u5efa\u7684\u96c6\u5408\u540d\u79f0<\/li>\n<li>options: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u6709\u5173\u5185\u5b58\u5927\u5c0f\u53ca\u7d22\u5f15\u7684\u9009\u9879<\/li>\n<\/ul>\n<p>options \u53ef\u4ee5\u662f\u5982\u4e0b\u53c2\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5b57\u6bb5<\/th>\n<th style=\"text-align: center;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">capped<\/td>\n<td style=\"text-align: center;\">\u5e03\u5c14<\/td>\n<td style=\"text-align: center;\">\uff08\u53ef\u9009\uff09\u5982\u679c\u4e3a true\uff0c\u5219\u521b\u5efa\u56fa\u5b9a\u96c6\u5408\u3002<br \/>\u56fa\u5b9a\u96c6\u5408\u662f\u6307\u6709\u7740\u56fa\u5b9a\u5927\u5c0f\u7684\u96c6\u5408\uff0c\u5f53\u8fbe\u5230\u6700\u5927\u503c\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u8986\u76d6\u6700\u65e9\u7684\u6587\u6863\u3002<br \/>\u5f53\u8be5\u503c\u4e3a true \u65f6\uff0c\u5fc5\u987b\u6307\u5b9a size \u53c2\u6570\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">autoIndexId<\/td>\n<td style=\"text-align: center;\">\u5e03\u5c14<\/td>\n<td style=\"text-align: center;\">\uff08\u53ef\u9009\uff09\u5982\u4e3a true\uff0c\u81ea\u52a8\u5728 _id \u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\u3002\u9ed8\u8ba4\u4e3a false\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">size<\/td>\n<td style=\"text-align: center;\">\u6570\u503c<\/td>\n<td style=\"text-align: center;\">\uff08\u53ef\u9009\uff09\u4e3a\u56fa\u5b9a\u96c6\u5408\u6307\u5b9a\u4e00\u4e2a\u6700\u5927\u503c\uff08\u4ee5\u5b57\u8282\u8ba1\uff09\u3002<br \/>\u5982\u679c capped \u4e3a true\uff0c\u4e5f\u9700\u8981\u6307\u5b9a\u8be5\u5b57\u6bb5\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">max<\/td>\n<td style=\"text-align: center;\">\u6570\u503c<\/td>\n<td style=\"text-align: center;\">\uff08\u53ef\u9009\uff09\u6307\u5b9a\u56fa\u5b9a\u96c6\u5408\u4e2d\u5305\u542b\u6587\u6863\u7684\u6700\u5927\u6570\u91cf\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u63d2\u5165\u6587\u6863\u65f6\uff0cMongoDB \u9996\u5148\u68c0\u67e5\u56fa\u5b9a\u96c6\u5408\u7684 size \u5b57\u6bb5\uff0c\u7136\u540e\u68c0\u67e5 max \u5b57\u6bb5\u3002<\/p>\n<h3>\u521b\u5efa\u96c6\u5408\u5b9e\u4f8b<\/h3>\n<p>\u5728 test \u6570\u636e\u5e93\u4e2d\u521b\u5efa appblog \u96c6\u5408\uff1a<\/p>\n<pre><code class=\"language-javascript\">> use test\nswitched to db test\n> db.createCollection(&quot;appblog&quot;)\n{ &quot;ok&quot; : 1 }<\/code><\/pre>\n<p>\u5982\u679c\u8981\u67e5\u770b\u5df2\u6709\u96c6\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528 show collections \u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"language-javascript\">> show collections\nappblog<\/code><\/pre>\n<p>\u4e0b\u9762\u662f\u5e26\u6709\u51e0\u4e2a\u5173\u952e\u53c2\u6570\u7684 createCollection() \u7684\u7528\u6cd5\uff1a<\/p>\n<p>\u521b\u5efa\u56fa\u5b9a\u96c6\u5408 mycol\uff0c\u6574\u4e2a\u96c6\u5408\u7a7a\u95f4\u5927\u5c0f 6142800 KB, \u6587\u6863\u6700\u5927\u4e2a\u6570\u4e3a 10000 \u4e2a\u3002<\/p>\n<pre><code class=\"language-javascript\">> db.createCollection(&quot;mycol&quot;, { capped : true, autoIndexId : true, size : 6142800, max : 10000 } )\n{\n    &quot;note&quot; : &quot;the autoIndexId option is deprecated and will be removed in a future release&quot;,\n    &quot;ok&quot; : 1\n}\n> show collections\nappblog\nmycol<\/code><\/pre>\n<p>\u5728 MongoDB \u4e2d\uff0c\u4e0d\u9700\u8981\u521b\u5efa\u96c6\u5408\u3002\u5f53\u63d2\u5165\u4e00\u4e9b\u6587\u6863\u65f6\uff0cMongoDB \u4f1a\u81ea\u52a8\u521b\u5efa\u96c6\u5408\u3002<\/p>\n<pre><code class=\"language-javascript\">> db.site.insert({&quot;name&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;})\nWriteResult({ &quot;nInserted&quot; : 1 })\n> show collections\nappblog\nmycol\nsite<\/code><\/pre>\n<h2>MongoDB\u5220\u9664\u96c6\u5408<\/h2>\n<h3>\u5220\u9664\u96c6\u5408\u8bed\u6cd5<\/h3>\n<p>MongoDB \u4e2d\u4f7f\u7528 drop() \u65b9\u6cd5\u6765\u5220\u9664\u96c6\u5408\u3002<\/p>\n<p>\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n<pre><code>db.collection.drop()<\/code><\/pre>\n<h3>\u5220\u9664\u96c6\u5408\u5b9e\u4f8b<\/h3>\n<pre><code class=\"language-javascript\">> show collections\nappblog\nmycol\nsite\n> db.mycol.drop()\ntrue\n> show collections\nappblog\nsite\n> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB\u521b\u5efa\u96c6\u5408 \u521b\u5efa\u96c6\u5408\u8bed\u6cd5 MongoDB \u4e2d\u4f7f\u7528 createCollection() \u65b9\u6cd5\u6765\u521b\u5efa [&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-1087","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1087","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=1087"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}