{"id":1086,"date":"2023-03-12T11:07:59","date_gmt":"2023-03-12T03:07:59","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1086"},"modified":"2023-04-29T11:41:12","modified_gmt":"2023-04-29T03:41:12","slug":"mongodb-database-basic-operations","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-database-basic-operations\/","title":{"rendered":"MongoDB\u6570\u636e\u5e93\u57fa\u672c\u64cd\u4f5c"},"content":{"rendered":"<h2>MongoDB\u8fde\u63a5<\/h2>\n<h3>\u6807\u51c6 URI \u8fde\u63a5\u8bed\u6cd5\uff1a<\/h3>\n<pre><code>mongodb:\/\/[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][\/[database][?options]]<\/code><\/pre>\n<p><!-- more --><\/p>\n<ul>\n<li><code>mongodb:\/\/<\/code> \u8fd9\u662f\u56fa\u5b9a\u7684\u683c\u5f0f\uff0c\u5fc5\u987b\u8981\u6307\u5b9a<\/li>\n<li><code>username:password@<\/code> \u53ef\u9009\u9879\uff0c\u5982\u679c\u8bbe\u7f6e\uff0c\u5728\u8fde\u63a5\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e4b\u540e\uff0c\u9a71\u52a8\u90fd\u4f1a\u5c1d\u8bd5\u767b\u9646\u8fd9\u4e2a\u6570\u636e\u5e93<\/li>\n<li><code>host1<\/code> \u5fc5\u987b\u7684\u6307\u5b9a\u81f3\u5c11\u4e00\u4e2ahost, host1 \u662f\u8fd9\u4e2aURI\u552f\u4e00\u8981\u586b\u5199\u7684\u3002\u5b83\u6307\u5b9a\u4e86\u8981\u8fde\u63a5\u670d\u52a1\u5668\u7684\u5730\u5740\u3002\u5982\u679c\u8981\u8fde\u63a5\u590d\u5236\u96c6\uff0c\u8bf7\u6307\u5b9a\u591a\u4e2a\u4e3b\u673a\u5730\u5740<\/li>\n<li><code>portX<\/code> \u53ef\u9009\u7684\u6307\u5b9a\u7aef\u53e3\uff0c\u5982\u679c\u4e0d\u586b\uff0c\u9ed8\u8ba4\u4e3a27017<\/li>\n<li><code>\/database<\/code> \u5982\u679c\u6307\u5b9ausername:password@\uff0c\u8fde\u63a5\u5e76\u9a8c\u8bc1\u767b\u9646\u6307\u5b9a\u6570\u636e\u5e93\u3002\u82e5\u4e0d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u6253\u5f00 test \u6570\u636e\u5e93\u3002<\/li>\n<li><code>?options<\/code> \u662f\u8fde\u63a5\u9009\u9879\u3002\u5982\u679c\u4e0d\u4f7f\u7528\/database\uff0c\u5219\u524d\u9762\u9700\u8981\u52a0\u4e0a\/\u3002\u6240\u6709\u8fde\u63a5\u9009\u9879\u90fd\u662f\u952e\u503c\u5bf9name=value\uff0c\u952e\u503c\u5bf9\u4e4b\u95f4\u901a\u8fc7&amp;\u6216;\uff08\u5206\u53f7\uff09\u9694\u5f00<\/li>\n<\/ul>\n<h3>URI \u8fde\u63a5\u5b9e\u4f8b<\/h3>\n<p>1\u3001\u4f7f\u7528\u9ed8\u8ba4\u7aef\u53e3\u8fde\u63a5 MongoDB \u7684\u670d\u52a1<\/p>\n<pre><code>> mongodb:\/\/localhost<\/code><\/pre>\n<p>2\u3001\u4f7f\u7528\u7528\u6237 admin \u548c\u5bc6\u7801 123456 \u8fde\u63a5\u5230\u672c\u5730\u7684 MongoDB \u670d\u52a1\u4e0a<\/p>\n<pre><code>> mongodb:\/\/admin:123456@localhost<\/code><\/pre>\n<p>3\u3001\u4f7f\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fde\u63a5\u767b\u9646\u5230\u6307\u5b9a\u6570\u636e\u5e93<\/p>\n<pre><code>> mongodb:\/\/admin:123456@localhost\/test<\/code><\/pre>\n<p>4\u3001\u8fde\u63a5 replica pair\uff0c\u670d\u52a1\u56681\u4e3aexample1.com\uff0c\u670d\u52a1\u56682\u4e3aexample2<\/p>\n<pre><code>> mongodb:\/\/example1.com:27017,example2.com:27017<\/code><\/pre>\n<p>5\u3001\u8fde\u63a5 replica set \u4e09\u53f0\u670d\u52a1\u5668 (\u7aef\u53e3 27017, 27018, \u548c27019)<\/p>\n<pre><code>> mongodb:\/\/localhost,localhost:27018,localhost:27019<\/code><\/pre>\n<p>6\u3001\u8fde\u63a5 replica set \u4e09\u53f0\u670d\u52a1\u5668\uff0c\u5199\u5165\u64cd\u4f5c\u5e94\u7528\u5728\u4e3b\u670d\u52a1\u5668\uff0c\u5e76\u4e14\u5206\u5e03\u67e5\u8be2\u5230\u4ece\u670d\u52a1\u5668<\/p>\n<pre><code>> mongodb:\/\/host1,host2,host3\/?slaveOk=true<\/code><\/pre>\n<p>7\u3001\u76f4\u63a5\u8fde\u63a5\u7b2c\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u65e0\u8bba\u662freplica set\u4e00\u90e8\u5206\u6216\u8005\u4e3b\u670d\u52a1\u5668\u6216\u8005\u4ece\u670d\u52a1\u5668<\/p>\n<pre><code>> mongodb:\/\/host1,host2,host3\/?connect=direct;slaveOk=true<\/code><\/pre>\n<p>\u5f53\u8fde\u63a5\u670d\u52a1\u5668\u6709\u4f18\u5148\u7ea7\uff0c\u8fd8\u9700\u8981\u5217\u51fa\u6240\u6709\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u8fde\u63a5\u65b9\u5f0f<\/p>\n<p>8\u3001\u5b89\u5168\u6a21\u5f0f\u8fde\u63a5\u5230localhost<\/p>\n<pre><code>> mongodb:\/\/localhost\/?safe=true<\/code><\/pre>\n<p>9\u3001\u4ee5\u5b89\u5168\u6a21\u5f0f\u8fde\u63a5\u5230replica set\uff0c\u5e76\u4e14\u7b49\u5f85\u81f3\u5c11\u4e24\u4e2a\u590d\u5236\u670d\u52a1\u5668\u6210\u529f\u5199\u5165\uff0c\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u4e3a2\u79d2<\/p>\n<pre><code>> mongodb:\/\/host1,host2,host3\/?safe=true;w=2;wtimeoutMS=2000<\/code><\/pre>\n<h2>MongoDB\u521b\u5efa\u6570\u636e\u5e93<\/h2>\n<h3>\u521b\u5efa\u6570\u636e\u5e93<\/h3>\n<h4>\u8bed\u6cd5<\/h4>\n<p>MongoDB \u521b\u5efa\u6570\u636e\u5e93\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>use DATABASE_NAME<\/code><\/pre>\n<p>\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u6570\u636e\u5e93\uff0c\u5426\u5219\u5207\u6362\u5230\u6307\u5b9a\u6570\u636e\u5e93\u3002<\/p>\n<p><!-- more --><\/p>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u521b\u5efa\u4e86\u6570\u636e\u5e93 appblog\uff1a<\/p>\n<pre><code>> use appblog\nswitched to db appblog\n> db\nappblog\n> <\/code><\/pre>\n<p>\u5982\u679c\u60f3\u67e5\u770b\u6240\u6709\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 show dbs \u547d\u4ee4\uff1a<\/p>\n<pre><code>> show dbs\nadmin   0.000GB\nconfig  0.000GB\nlocal   0.000GB\ntest    0.000GB\n> <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u521a\u521b\u5efa\u7684\u6570\u636e\u5e93 appblog \u5e76\u4e0d\u5728\u6570\u636e\u5e93\u7684\u5217\u8868\u4e2d\uff0c\u8981\u663e\u793a\u5b83\uff0c\u6211\u4eec\u9700\u8981\u5411 appblog \u6570\u636e\u5e93\u63d2\u5165\u4e00\u4e9b\u6570\u636e\u3002<\/p>\n<pre><code>> db.site.insert({&quot;name&quot;: &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;})\nWriteResult({ &quot;nInserted&quot; : 1 })\n> show dbs\nadmin    0.000GB\nappblog  0.000GB\nconfig   0.000GB\nlocal    0.000GB\ntest     0.000GB\n> <\/code><\/pre>\n<p>MongoDB \u4e2d\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u4e3a test\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u521b\u5efa\u65b0\u7684\u6570\u636e\u5e93\uff0c\u96c6\u5408\u5c06\u5b58\u653e\u5728 test \u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<h2>MongoDB\u5220\u9664\u6570\u636e\u5e93<\/h2>\n<h3>\u5220\u9664\u6570\u636e\u5e93<\/h3>\n<h4>\u8bed\u6cd5<\/h4>\n<p>MongoDB \u5220\u9664\u6570\u636e\u5e93\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>db.dropDatabase()<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5220\u9664\u5f53\u524d\u6570\u636e\u5e93\uff0c\u9ed8\u8ba4\u4e3a test\uff0c\u53ef\u4ee5\u4f7f\u7528 db \u547d\u4ee4\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u540d\u3002<\/p>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6211\u4eec\u5220\u9664\u6570\u636e\u5e93 appblog<\/p>\n<p>\u9996\u5148\uff0c\u67e5\u770b\u6240\u6709\u6570\u636e\u5e93\uff1a<\/p>\n<pre><code>> show dbs\nadmin    0.000GB\nappblog  0.000GB\nconfig   0.000GB\nlocal    0.000GB\ntest     0.000GB<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5207\u6362\u5230\u6570\u636e\u5e93 appblog<\/p>\n<pre><code>> use appblog\nswitched to db appblog<\/code><\/pre>\n<p>\u6267\u884c\u5220\u9664\u547d\u4ee4\uff1a<\/p>\n<pre><code>> db.dropDatabase()\n{ &quot;dropped&quot; : &quot;appblog&quot;, &quot;ok&quot; : 1 }<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u518d\u901a\u8fc7 show dbs \u547d\u4ee4\u6570\u636e\u5e93\u662f\u5426\u5220\u9664\u6210\u529f\uff1a<\/p>\n<pre><code>> show dbs\nadmin   0.000GB\nconfig  0.000GB\nlocal   0.000GB\ntest    0.000GB<\/code><\/pre>\n<h3>\u5220\u9664\u96c6\u5408<\/h3>\n<p>\u96c6\u5408\u5220\u9664\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>db.collection.drop()<\/code><\/pre>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5220\u9664 appblog \u6570\u636e\u5e93\u4e2d\u7684\u96c6\u5408 site\uff1a<\/p>\n<pre><code>> use appblog\nswitched to db appblog\n> db.site.insert({&quot;name&quot;: &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;})\nWriteResult({ &quot;nInserted&quot; : 1 })\n> show tables\nsite\n> db.site.drop()\ntrue\n> show tables\n> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB\u8fde\u63a5 \u6807\u51c6 URI \u8fde\u63a5\u8bed\u6cd5\uff1a mongodb:\/\/[username:password@]ho [&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-1086","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1086","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=1086"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1086\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}