{"id":1494,"date":"2018-05-31T16:51:40","date_gmt":"2018-05-31T08:51:40","guid":{"rendered":"https:\/\/www.strongd.net\/?p=1494"},"modified":"2018-05-31T16:51:40","modified_gmt":"2018-05-31T08:51:40","slug":"mongodb-%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c-%e5%a4%87%e4%bb%bd-%e8%bf%98%e5%8e%9f-%e5%af%bc%e5%87%ba-%e5%af%bc%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1494","title":{"rendered":"mongodb \u6570\u636e\u5e93\u64cd\u4f5c&#8211;\u5907\u4efd \u8fd8\u539f \u5bfc\u51fa \u5bfc\u5165"},"content":{"rendered":"<div class=\"art_desc mt10\">\n<div id=\"art_demo\">mongodb\u6570\u636e\u5907\u4efd\u548c\u8fd8\u539f\u4e3b\u8981\u5206\u4e3a\u4e8c\u79cd\uff0c\u4e00\u79cd\u662f\u9488\u5bf9\u4e8e\u5e93\u7684mongodump\u548cmongorestore\uff0c\u4e00\u79cd\u662f\u9488\u5bf9\u5e93\u4e2d\u8868\u7684mongoexport\u548cmongoimport\u3002<\/div>\n<\/div>\n<div class=\"lbd clearfix\"><\/div>\n<div id=\"content\">\n<p>\u4e00\uff0cmongodump\u5907\u4efd\u6570\u636e\u5e93<\/p>\n<p>1\uff0c\u5e38\u7528\u547d\u4ee4\u683c<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">mongodump -h IP --port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 -o \u6587\u4ef6\u5b58\u5728\u8def\u5f84 \r\n<\/pre>\n<\/div>\n<p>\u5982\u679c\u6ca1\u6709\u7528\u6237\u8c01\uff0c\u53ef\u4ee5\u53bb\u6389-u\u548c-p\u3002<br \/>\n\u5982\u679c\u5bfc\u51fa\u672c\u673a\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u53bb\u6389-h\u3002<br \/>\n\u5982\u679c\u662f\u9ed8\u8ba4\u7aef\u53e3\uff0c\u53ef\u4ee5\u53bb\u6389&#8211;port\u3002<br \/>\n\u5982\u679c\u60f3\u5bfc\u51fa\u6240\u6709\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u53bb\u6389-d\u3002<\/p>\n<p>2,\u5bfc\u51fa\u6240\u6709\u6570\u636e\u5e93<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongodump -h 127.0.0.1 -o \/home\/zhangy\/mongodb\/ \r\nconnected to: 127.0.0.1 \r\nTue Dec 3 06:15:55.448 all dbs \r\nTue Dec 3 06:15:55.449 DATABASE: test   to   \/home\/zhangy\/mongodb\/test \r\nTue Dec 3 06:15:55.449   test.system.indexes to \/home\/zhangy\/mongodb\/test\/system.indexes.bson \r\nTue Dec 3 06:15:55.450     1 objects \r\nTue Dec 3 06:15:55.450   test.posts to \/home\/zhangy\/mongodb\/test\/posts.bson \r\nTue Dec 3 06:15:55.480     0 objects \r\n \r\n\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u7701\u7565\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002 \r\n\r\n<\/pre>\n<\/div>\n<p>3,\u5bfc\u51fa\u6307\u5b9a\u6570\u636e\u5e93<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongodump -h 192.168.1.108 -d tank -o \/home\/zhangy\/mongodb\/ \r\nconnected to: 192.168.1.108 \r\nTue Dec 3 06:11:41.618 DATABASE: tank   to   \/home\/zhangy\/mongodb\/tank \r\nTue Dec 3 06:11:41.623   tank.system.indexes to \/home\/zhangy\/mongodb\/tank\/system.indexes.bson \r\nTue Dec 3 06:11:41.623     2 objects \r\nTue Dec 3 06:11:41.623   tank.contact to \/home\/zhangy\/mongodb\/tank\/contact.bson \r\nTue Dec 3 06:11:41.669     2 objects \r\nTue Dec 3 06:11:41.670   Metadata for tank.contact to \/home\/zhangy\/mongodb\/tank\/contact.metadata.json \r\nTue Dec 3 06:11:41.670   tank.users to \/home\/zhangy\/mongodb\/tank\/users.bson \r\nTue Dec 3 06:11:41.685     2 objects \r\nTue Dec 3 06:11:41.685   Metadata for tank.users to \/home\/zhangy\/mongodb\/tank\/users.metadata.json \r\n\r\n<\/pre>\n<\/div>\n<p>\u4e09\uff0cmongorestore\u8fd8\u539f\u6570\u636e\u5e93<\/p>\n<p>1,\u5e38\u7528\u547d\u4ee4\u683c\u5f0f<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">mongorestore -h IP --port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 --drop \u6587\u4ef6\u5b58\u5728\u8def\u5f84<\/pre>\n<\/div>\n<p>&#8211;drop\u7684\u610f\u601d\u662f\uff0c\u5148\u5220\u9664\u6240\u6709\u7684\u8bb0\u5f55\uff0c\u7136\u540e\u6062\u590d\u3002<\/p>\n<p>2,\u6062\u590d\u6240\u6709\u6570\u636e\u5e93\u5230mongodb\u4e2d<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongorestore \/home\/zhangy\/mongodb\/  #\u8fd9\u91cc\u7684\u8def\u5f84\u662f\u6240\u6709\u5e93\u7684\u5907\u4efd\u8def\u5f84<\/pre>\n<\/div>\n<p>3,\u8fd8\u539f\u6307\u5b9a\u7684\u6570\u636e\u5e93<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongorestore -d tank \/home\/zhangy\/mongodb\/tank\/  #tank\u8fd9\u4e2a\u6570\u636e\u5e93\u7684\u5907\u4efd\u8def\u5f84 \r\n \r\n[root@localhost mongodb]# mongorestore -d tank_new \/home\/zhangy\/mongodb\/tank\/  #\u5c06tank\u8fd8\u6709tank_new\u6570\u636e\u5e93\u4e2d<\/pre>\n<\/div>\n<p>\u8fd9\u4e8c\u4e2a\u547d\u4ee4\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u5907\u4efd\u4e0e\u8fd8\u539f\uff0c\u6587\u4ef6\u683c\u5f0f\u662fjson\u548cbson\u7684\u3002\u65e0\u6cd5\u6307\u5199\u5230\u8868\u5907\u4efd\u6216\u8005\u8fd8\u539f\u3002<\/p>\n<p>\u56db\uff0cmongoexport\u5bfc\u51fa\u8868\uff0c\u6216\u8005\u8868\u4e2d\u90e8\u5206\u5b57\u6bb5<\/p>\n<p>1,\u5e38\u7528\u547d\u4ee4\u683c\u5f0f<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:plain;\">mongoexport -h IP --port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 -c \u8868\u540d -f \u5b57\u6bb5 -q \u6761\u4ef6\u5bfc\u51fa --csv -o \u6587\u4ef6\u540d \r\n<\/pre>\n<\/div>\n<p>\u4e0a\u9762\u7684\u53c2\u6570\u597d\u7406\u89e3\uff0c\u91cd\u70b9\u8bf4\u4e00\u4e0b\uff1a<br \/>\n-f\u00a0\u00a0\u00a0 \u5bfc\u51fa\u6307\u5b57\u6bb5\uff0c\u4ee5\u5b57\u53f7\u5206\u5272\uff0c-f name,email,age\u5bfc\u51faname,email,age\u8fd9\u4e09\u4e2a\u5b57\u6bb5<br \/>\n-q\u00a0\u00a0\u00a0 \u53ef\u4ee5\u6839\u67e5\u8be2\u6761\u4ef6\u5bfc\u51fa\uff0c-q &#8216;{ &#8220;uid&#8221; : &#8220;100&#8221; }&#8217; \u5bfc\u51fauid\u4e3a100\u7684\u6570\u636e<br \/>\n&#8211;csv \u8868\u793a\u5bfc\u51fa\u7684\u6587\u4ef6\u683c\u5f0f\u4e3acsv\u7684\uff0c\u8fd9\u4e2a\u6bd4\u8f83\u6709\u7528\uff0c\u56e0\u4e3a\u5927\u90e8\u5206\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u90fd\u662f\u652f\u6301csv\uff0c\u5728\u8fd9\u91cc\u6709\u5171\u540c\u70b9<\/p>\n<p>2,\u5bfc\u51fa\u6574\u5f20\u8868<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongoexport -d tank -c users -o \/home\/zhangy\/mongodb\/tank\/users.dat \r\nconnected to: 127.0.0.1 \r\nexported 4 records \r\n\r\n<\/pre>\n<\/div>\n<p>3,\u5bfc\u51fa\u8868\u4e2d\u90e8\u5206\u5b57\u6bb5<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongoexport -d tank -c users --csv -f uid,name,sex -o tank\/users.csv \r\nconnected to: 127.0.0.1 \r\nexported 4 records \r\n\r\n<\/pre>\n<\/div>\n<p>4,\u6839\u636e\u6761\u4ef6\u6562\u51fa\u6570\u636e<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongoexport -d tank -c users -q '{uid:{$gt:1}}' -o tank\/users.json \r\nconnected to: 127.0.0.1 \r\nexported 3 records \r\n\r\n<\/pre>\n<\/div>\n<p>\u4e94\uff0cmongoimport\u5bfc\u5165\u8868\uff0c\u6216\u8005\u8868\u4e2d\u90e8\u5206\u5b57\u6bb5<\/p>\n<p>1,\u5e38\u7528\u547d\u4ee4\u683c\u5f0f<\/p>\n<p>1.1,\u8fd8\u539f\u6574\u8868\u5bfc\u51fa\u7684\u975ecsv\u6587\u4ef6<br \/>\nmongoimport -h IP &#8211;port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 -c \u8868\u540d &#8211;upsert &#8211;drop \u6587\u4ef6\u540d<br \/>\n\u91cd\u70b9\u8bf4\u4e00\u4e0b&#8211;upsert\uff0c\u5176\u4ed6\u53c2\u6570\u4e0a\u9762\u7684\u547d\u4ee4\u5df2\u6709\u63d0\u5230\uff0c&#8211;upsert \u63d2\u5165\u6216\u8005\u66f4\u65b0\u73b0\u6709\u6570\u636e<br \/>\n1.2,\u8fd8\u539f\u90e8\u5206\u5b57\u6bb5\u7684\u5bfc\u51fa\u6587\u4ef6<br \/>\nmongoimport -h IP &#8211;port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 -c \u8868\u540d &#8211;upsertFields \u5b57\u6bb5 &#8211;drop \u6587\u4ef6\u540d<br \/>\n&#8211;upsertFields\u6839&#8211;upsert\u4e00\u6837<br \/>\n1.3,\u8fd8\u539f\u5bfc\u51fa\u7684csv\u6587\u4ef6<br \/>\nmongoimport -h IP &#8211;port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 -c \u8868\u540d &#8211;type \u7c7b\u578b &#8211;headerline &#8211;upsert &#8211;drop \u6587\u4ef6\u540d<br \/>\n\u4e0a\u9762\u4e09\u79cd\u60c5\u51b5\uff0c\u8fd8\u53ef\u4ee5\u6709\u5176\u4ed6\u6392\u5217\u7ec4\u5408\u7684\u3002<\/p>\n<p>2,\u8fd8\u539f\u5bfc\u51fa\u7684\u8868\u6570\u636e<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">[root@localhost mongodb]# mongoimport -d tank -c users --upsert tank\/users.dat \r\nconnected to: 127.0.0.1 \r\nTue Dec 3 08:26:52.852 imported 4 objects\r\n\r\n<\/pre>\n<\/div>\n<p>3,\u90e8\u5206\u5b57\u6bb5\u7684\u8868\u6570\u636e\u5bfc\u5165<\/p>\n<p>[root@localhost mongodb]# mongoimport -d tank -c users\u00a0 &#8211;upsertFields uid,name,sex\u00a0 tank\/users.dat<br \/>\nconnected to: 127.0.0.1<br \/>\nTue Dec\u00a0 3 08:31:15.179 imported 4 objects<\/p>\n<p>4,\u8fd8\u539fcsv\u6587\u4ef6<\/p>\n<div class=\"jb51code\">\n<pre class=\"brush:plain;\">[root@localhost mongodb]# mongoimport -d tank -c users --type csv --headerline --file tank\/users.csv \r\nconnected to: 127.0.0.1 \r\nTue Dec 3 08:37:21.961 imported 4 objects \r\n\r\n<\/pre>\n<\/div>\n<p>\u603b\u4f53\u611f\u89c9\uff0cmongodb\u7684\u5907\u4efd\u4e0e\u8fd8\u539f\uff0c\u8fd8\u662f\u633a\u5f3a\u5927\u7684\uff0c\u867d\u7136\u6709\u70b9\u9ebb\u70e6\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>mongodb\u6570\u636e\u5907\u4efd\u548c\u8fd8\u539f\u4e3b\u8981\u5206\u4e3a\u4e8c\u79cd\uff0c\u4e00\u79cd\u662f\u9488\u5bf9\u4e8e\u5e93\u7684mongodump\u548cmongorestore\uff0c\u4e00\u79cd\u662f\u9488\u5bf9\u5e93\u4e2d\u8868\u7684mongoexport\u548cmongoimport\u3002 \u4e00\uff0cmongodump\u5907\u4efd\u6570\u636e\u5e93 1\uff0c\u5e38\u7528\u547d\u4ee4\u683c mongodump -h IP &#8211;port \u7aef\u53e3 -u \u7528\u6237\u540d -p \u5bc6\u7801 -d \u6570\u636e\u5e93 -o \u6587\u4ef6\u5b58\u5728\u8def\u5f84 \u5982\u679c\u6ca1\u6709\u7528\u6237\u8c01\uff0c\u53ef\u4ee5\u53bb\u6389-u\u548c-p\u3002 \u5982\u679c\u5bfc\u51fa\u672c\u673a\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u53bb\u6389-h\u3002 \u5982\u679c\u662f\u9ed8\u8ba4\u7aef\u53e3\uff0c\u53ef\u4ee5\u53bb\u6389&#8211;port\u3002 \u5982\u679c\u60f3\u5bfc\u51fa\u6240\u6709\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u53bb\u6389-d\u3002 2,\u5bfc\u51fa\u6240\u6709\u6570\u636e\u5e93 [root@localhost mongodb]# mongodump -h 127.0.0.1 -o \/home\/zhangy\/mongodb\/ connected to: 127.0.0.1 Tue Dec 3 06:15:55.448 all dbs Tue Dec 3 06:15:55.449 DATABASE: test to \/home\/zhangy\/mongodb\/test Tue Dec 3 06:15:55.449 test.system.indexes to \/home\/zhangy\/mongodb\/test\/system.indexes.bson Tue Dec 3 &hellip; <a href=\"https:\/\/www.strongd.net\/?p=1494\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">mongodb \u6570\u636e\u5e93\u64cd\u4f5c&#8211;\u5907\u4efd \u8fd8\u539f \u5bfc\u51fa \u5bfc\u5165<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[234,20],"tags":[10],"class_list":["post-1494","post","type-post","status-publish","format-standard","hentry","category-mongodb","category-20","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1494"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1494\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1494\/revisions\/1495"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}