{"id":204,"date":"2021-02-10T10:11:24","date_gmt":"2021-02-10T02:11:24","guid":{"rendered":"http:\/\/gjweb.top\/?p=204"},"modified":"2022-03-29T17:57:39","modified_gmt":"2022-03-29T09:57:39","slug":"%e5%9f%ba%e6%9c%ac%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=204","title":{"rendered":"mongoDB\u57fa\u672c\u547d\u4ee4(\u65e7\u7248)"},"content":{"rendered":"\n<ul class=\"wp-block-list\"><li>mongod \u542f\u52a8\u670d\u52a1<\/li><li>db \u67e5\u770b\u5f53\u524d\u64cd\u4f5c\u90a3\u4e2a\u4e00\u4e2a\u6570\u636e\u5e93<\/li><li>show dbs \u67e5\u770b\u6570\u636e\u6570\u636e\u5217\u8868(\u4e00\u5171\u6709\u51e0\u4e2a\u6570\u636e\u5e93,\u5907\u6ce8: \u5982\u679c\u6570\u636e\u5e93\u4e3a\u7a7a,\u4e0d\u51fa\u73b0\u5728\u5217\u8868\u4e2d)<\/li><li>use test : \u5207\u6362\u5230test\u6570\u636e\u5e93,\u5982\u679c\u4e0d\u5b58\u5728,\u5219\u521b\u5efa\u4e00\u4e2atest\u5e93<\/li><li>db.students.insert() \u5411\u5f53\u524d\u6570\u636e\u5e93\u7684students\u96c6\u5408\u4e2d\u63d2\u5165\u4e00\u4e2a\u6587\u6863(\u6ca1\u6709\u91cd\u65b0\u521b\u5efa,\u5728\u63d2\u5165)<\/li><li>show collections \u5c55\u793a\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u6240\u6709\u7684\u96c6\u5408<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7528\u6237\u521b\u5efa\u53ca\u7528\u6237\u6743\u9650\">\u7528\u6237\u521b\u5efa\u53ca\u7528\u6237\u6743\u9650<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u521b\u5efa\u7528\u6237\">\u521b\u5efa\u7528\u6237<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>db.createUser(<br>    {<br>        user:\"admin\",<br>        pwd:\"123456\",<br>        roles:&#91;{role:'userAdminAnyDatabase',db:\"admin\"}]<br>    }<br>)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u7528\u6237\u8ba4\u8bc1\">\u7528\u6237\u8ba4\u8bc1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>use admin<br>db.auth(\"admin\",\"123456\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5185\u7f6e\u6743\u9650\u8bf4\u660e\">\u5185\u7f6e\u6743\u9650\u8bf4\u660e<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6570\u636e\u5e93\u7528\u6237\u89d2\u8272<ul><li>read \u53ea\u8bfb\u6743\u9650<\/li><li>readWrite \u8bfb\u5199\u6743\u9650<\/li><\/ul><\/li><li>\u6570\u636e\u5e93\u7ba1\u7406\u89d2\u8272<ul><li>dbAdmin \u5728\u5f53\u524ddb\u4e2d\u6267\u884c\u7ba1\u7406\u64cd\u4f5c\u7684\u6743\u9650<\/li><li>dbOwner \u5728\u5f53\u524ddb\u4e2d\u6267\u884c\u4efb\u610f\u64cd\u4f5c<\/li><li>userADmin \u5728\u5f53\u524ddb\u4e2d\u7ba1\u7406user\u7684\u6743\u9650<\/li><\/ul><\/li><li>\u5907\u4efd\u548c\u8fd8\u539f\u89d2\u8272<ul><li>backup<\/li><li>restore<\/li><\/ul><\/li><li>\u5938\u5e93\u89d2\u8272<ul><li>clusterAdmin \u7ba1\u7406\u673a\u5668\u7684\u6700\u9ad8\u6743\u9650<\/li><li>clusterManager \u7ba1\u7406\u548c\u76d1\u63a7\u96c6\u7fa4\u7684\u6743\u9650<\/li><li>clusterMonitor \u76d1\u63a7\u96c6\u7fa4\u7684\u6743\u9650<\/li><li>hostManager \u7ba1\u7406server<\/li><\/ul><\/li><li>\u8d85\u7ea7\u6743\u9650<ul><li>root \u8d85\u7ea7\u7528\u6237<\/li><\/ul><\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/docs.mongodb.com\/manual\/tutorial\/enable-authentication\/\">mongoDB\u5b98\u65b9\u6587\u6863<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6570\u636e\u5e93\u7684\u5907\u4efd\u4e0e\u6062\u590d-docker\u73af\u5883\">\u6570\u636e\u5e93\u7684\u5907\u4efd\u4e0e\u6062\u590d(docker\u73af\u5883)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5907\u4efd\">\u5907\u4efd<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u67e5\u770b\u5bb9\u5668\u540d\u79f0\u548c\u5bb9\u5668code<br>docker ps | grep mongotest<br>\/\/ \u5907\u4efd<br>docker exec -it mongotest_mongo_1 mongodump -h localhost -u root -p exaple -o \/tmp\/test<br>\/*<br>    * -h \u9700\u8981\u8fde\u63a5\u6570\u636e\u5e93\u7684\u5730\u5740<br>    * -u \u662f\u6570\u636e\u5e93\u7528\u6237<br>    * -p \u7528\u6237\u5bc6\u7801<br>    * -o \u8868\u793a\u5907\u4efd\u540e\u7684\u4f4d\u7f6e<br>    * -d \u6307\u5b9a\u5907\u4efd\u7684\u6570\u636e\u5e93,\u5982\u679c\u4e0d\u586b\u5c06\u5907\u4efd\u7528\u6237\u4e0b\u9762\u7684\u6240\u6709\u5e93<br>*\/<br>\/\/ \u5c06docker\u5bb9\u5668\u5185\u90e8\u7684\u6587\u4ef6\u62f7\u8d1d\u51fa\u6765<br>docker cp \u5bb9\u5668code:\/tmp\/test \/tmp\/test<br>\u200b<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6062\u590d\">\u6062\u590d<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec -it mongotest_mongo_1 mongoerstore -h localhost -u root -p example --dir \/tmp\/test\n\/*\n    * --dir \u8868\u793a\u4ece\u90a3\u4e2a\u5907\u4efd\u8def\u5f84\u4e2d\u6062\u590d\n*\/\n---<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u589e\">\u589e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>db.\u96c6\u5408\u540d.insert(\u6587\u6863\u5bf9\u8c61)\ndb.\u96c6\u5408\u540d.insertOne(\u6587\u6863\u5bf9\u8c61)\ndb.\u96c6\u5408\u540d.insertMany(&#91;\u6587\u6863\u5bf9\u8c61,\u6587\u6863\u5bf9\u8c61])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u67e5\">\u67e5<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>db.\u96c6\u5408.find(\u67e5\u8be2\u6761\u4ef6[,\u6295\u5f71])<ul><li>\u4e3e\u4f8b<ul><li>db.students.find({age:18}) \u67e5\u627e\u5e74\u9f84\u4e3a18\u7684\u6240\u6709\u4fe1\u606f<\/li><\/ul><\/li><li>\u4e3e\u4f8b<ul><li>db.students.find({age:18,name:&#8217;jack&#8217;}) \u67e5\u627e\u5e74\u9f84\u4e3a18 \u4e14\u540d\u5b57\u4e3aJack\u7684\u5b66\u751f<\/li><\/ul><\/li><\/ul><\/li><li>\u5e38\u89c4\u64cd\u4f5c\u7b26<ul><li>&lt; , &lt;= , &gt; , &gt;= , !== \u5bf9\u5e94 $lt , $lte , $gt , $gte , $ne<ul><li>\u4e3e\u4f8b<ul><li>db.\u96c6\u5408\u540d.find({age:{$get:20}}) \u5e74\u9f84\u662f\u5927\u4e8e\u7b49\u4e8e20\u7684<\/li><\/ul><\/li><\/ul><\/li><li>\u903b\u8f91\u6216; \u4f7f\u7528 $in \u6216 $or<ul><li>\u67e5\u627e\u5e74\u9f84\u4e3a18\u621620\u7684\u5b66\u751f<ul><li>\u4e3e\u4f8b<ul><li>db.stydents.find({age:{$in:[18,20]}})<\/li><\/ul><\/li><li>\u4e3e\u4f8b<ul><li>db.students.find({$or:[{age:18},{age:20}]})<\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><li>\u903b\u8f91\u975e: $nin<\/li><li>\u6b63\u5219\u5339\u914d<ul><li>\u4e3e\u4f8b<ul><li>db.students.find({name:\/^T\/})<\/li><\/ul><\/li><\/ul><\/li><li>$where \u80fd\u5199\u51fd\u6570<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">db.students.find({$where:function(){<br> &nbsp; &nbsp;return this.name === \"zhangsan\" &amp;&amp; this.age === 18<br>}})<\/pre>\n\n\n\n<p>\u8865\u5145:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6295\u5f71<ul><li>\u8fc7\u6ee4\u6389\u4e0d\u60f3\u8981\u7684\u6570\u636e,\u53ea\u4fdd\u7559\u60f3\u8981\u7684\u5c55\u793a\u7684\u6570\u636e<\/li><li>\u4e3e\u4f8b<ul><li>db.studentents.find({},{_id:0,name:0}), \u8fc7\u6ee4\u6389 id \u548c name<\/li><\/ul><\/li><li>\u4e3e\u4f8b<ul><li>db.students.find({},{age:1}) \u53ea\u4fdd\u7559age<\/li><\/ul><\/li><\/ul><\/li><li>db.\u96c6\u5408\u540d.findOne(\u67e5\u8be2\u6761\u4ef6[,\u6295\u5f71]), \u9ed8\u8ba4\u53ea\u8981\u627e\u5230\u4e00\u4e2a<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u66f4\u65b0\">\u66f4\u65b0<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">db.\u96c6\u5408\u540d.update(\u67e5\u8be2\u6761\u4ef6,\u66f4\u65b0\u7684\u5185\u5bb9[,\u914d\u7f6e\u5bf9\u8c61])<br>\/\/ \u5982\u4e0b\u5199\u6cd5\u4f1a\u5c06\u66f4\u65b0\u5185\u5bb9\u66ff\u6362\u6389\u6574\u4e2a\u6587\u6863\u5bf9\u8c61,\u4f46_id\u4e0d\u53d7\u5f71\u54cd<br>db.students.update({name:'zhangsan'},{age:19})<br>\/\/ \u4f7f\u7528 $set \u4fee\u6539\u6307\u5b9a\u5185\u5bb9,\u5176\u4ed6\u6570\u636e\u4e0d\u53d8,\u4e0d\u8fc7\u53ea\u80fd\u5339\u914d\u4e00\u4e2a zhangsan<br>db.students.update({name:\"zhangsan\"},{age:19})<br>\/\/ \u4fee\u6539\u591a\u4e2a\u6587\u6863\u5bf9\u8c61,\u5339\u914d\u591a\u4e2azhangsan ,\u628a\u6240\u6709zhangsan\u7684\u5e74\u9f84\u90fd\u66ff\u6362\u4e3a19<br>db.students.update({name:\"zhangsan\"},{$set:{age:19}})<br>\/\/\u4fee\u6539\u591a\u4e2a\u6587\u6863\u5bf9\u8c61,\u5339\u914d\u591a\u4e2azahngsan\u7684\u5e74\u9f84\u90fd\u66ff\u6362\u4e3a19<br>db.students.update({name:\"zhangsan\"},{$set:{age:19}},{multi:true})<\/pre>\n\n\n\n<p>\u8865\u5145<\/p>\n\n\n\n<p>db.\u96c6\u5408\u540d.updateOne(\u67e5\u8be2\u6761\u4ef6,\u8981\u66f4\u65b0\u7684\u5185\u5bb9[,\u914d\u7f6e\u5bf9\u8c61])<\/p>\n\n\n\n<p>db.\u96c6\u5408\u540d.updateMany(\u67e5\u8be2\u6761\u4ef6,\u66f4\u65b0\u7684\u5185\u5bb9[,\u914d\u7f6e\u5bf9\u8c61])<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5220\u9664\">\u5220\u9664<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*<br>    db.\u96c6\u5408\u540d.remove(\u67e5\u8be2\u6761\u4ef6)<br>        \/\/ \u5220\u9664\u6240\u6709\u5e74\u9f84\u5c0f\u4e8e\u7b49\u4e8e19\u7684\u5b66\u751f<br>*\/<br>db.students.remove({age:{$lte:19}})<br>\u200b<br>db.students.remove({age:20})<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>mongod \u542f\u52a8\u670d\u52a1 db \u67e5\u770b\u5f53\u524d\u64cd\u4f5c\u90a3\u4e2a\u4e00\u4e2a\u6570\u636e\u5e93 show dbs \u67e5\u770b\u6570\u636e\u6570\u636e\u5217\u8868(\u4e00\u5171\u6709\u51e0\u4e2a\u6570\u636e\u5e93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-monggodb"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":4,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/388"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}