{"id":5404,"date":"2016-10-04T18:23:16","date_gmt":"2016-10-04T10:23:16","guid":{"rendered":"https:\/\/sdeno.com\/?p=5404"},"modified":"2017-04-06T17:09:47","modified_gmt":"2017-04-06T09:09:47","slug":"express%e4%b8%8emongodb%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5404","title":{"rendered":"express\u4e0eMongoDB\u4ea4\u4e92"},"content":{"rendered":"<p>MongoDB\u670d\u52a1\u5668\u7aef\u5b89\u88c5\u914d\u7f6e\u597d\u540e\uff0c\u5c31\u8981\u4e0e\u5ba2\u6237\u7aef\u4ea4\u4e92\uff0c\u4f8b\u5982php\u64cd\u4f5cmongodb\u3001express\u64cd\u4f5cmongodb\uff0c\u7b49\u7b49\u3002<\/p>\n<p>\u8fd9\u91cc\u4e3b\u8981\u4ecb\u7ecdexpress\u4e0emongodb\u4ea4\u4e92\u3002\u5047\u8bbe\u5df2\u7ecf\u5b89\u88c5\u597dexpress\u548cnode.js\uff0c\u5269\u4e0b\u5c31\u5b89\u88c5node.js\u4e0b\u7684mongodb\u5ba2\u6237\u7aef\u6a21\u5757\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>1\uff0c\u9996\u5148\u5b89\u88c5mongodb\u5ba2\u6237\u7aef<\/p>\n<pre>npm install mongodb<\/pre>\n<p>&nbsp;<\/p>\n<p>2\uff0c\u5b89\u88c5\u597dmongodb\u6a21\u5757\u540e\uff0c\u5f00\u59cb\u8c03\u7528\uff0c\u5982\uff1a<\/p>\n<pre>var MongoClient = require('mongodb').MongoClient;\r\nvar DB_CONN_STR = 'mongodb:\/\/xgllseo:123@localhost:27017\/xgllseo';  \/\/\u5f00\u542fauth\u65f6\u4f7f\u7528,\u5c31\u662f\u4e0d\u9700\u8981\u5e10\u53f7\u5bc6\u7801\r\nmongodb:\/\/\u5e10\u53f7:\u5bc6\u7801@IP\u5730\u5740:\u7aef\u53e3\/\u6570\u636e\u5e93\r\n\r\nvar DB_CONN_STR = 'mongodb:\/\/localhost:27017\/wilsondb1';  \/\/\u5982\u679c\u6ca1\u8bbe\u7f6e\u5bc6\u7801\u5c31\u4f7f\u7528<\/pre>\n<p>\u73b0\u5728\u5df2\u7ecf\u53ef\u4ee5\u4f7f\u7528express\u64cd\u4f5cmongodb\u6570\u636e\u5e93\u4e86<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u589e<\/span>\uff1a<\/p>\n<pre>MongoClient.connect(DB_CONN_STR, function(err1, db) {\r\n  if(err1){\r\n       console.log('\u8fde\u63a5\u6570\u636e\u5e93\u5931\u8d25');\r\n  }else{\r\n       db.collection('xx22').insert([{\"name\":'wilson001',\"age\":21}],function (err2, result) {\r\n          if(err2){\r\n              console.log('\u63d2\u5165\u6570\u636e\u5931\u8d25');\r\n          }else{\r\n              console.log(result); \/\/\u663e\u793a\u63d2\u5165\u6210\u529f\u540e\u7684\u6570\u636e\r\n              db.close();\r\n          }\r\n       });\r\n  }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5220<\/span>\uff1a<\/p>\n<pre>\/\/\u67e5\u627e\u5230{\"name\":'wilson002'}\u6570\u636e\u540e\uff0c\u5c31\u5220\u9664\u6b64\u6761\u6570\u636e\r\nMongoClient.connect(DB_CONN_STR, function(err1, db) {\r\n   if(err1){\r\n       console.log('\u8fde\u63a5\u6570\u636e\u5e93\u5931\u8d25');\r\n   }else{\r\n       db.collection('xx').remove({\"name\":'wilson002'},function (err2,result) {\r\n           if(err2){\r\n                console.log('\u5220\u9664\u6570\u636e\u5931\u8d25');\r\n           }else{\r\n                console.log('\u5220\u9664\u6570\u636e\u6210\u529f');\r\n           }\r\n       });\r\n    }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u6539<\/span>\uff1a<\/p>\n<pre>\/\/\u5148\u67e5\u627e{\"name\":'wilson001'}\u6709\u6ca1\u6709\u6570\u636e\uff0c\u5982\u679c\u6709\u5c31\u4fee\u6539\u6570\u636e\u4e3a{ \"name\" : \"\u4fee\u6539\u540e\" }\r\nMongoClient.connect(DB_CONN_STR, function(err1, db) {\r\n   if(err1){\r\n       console.log('\u8fde\u63a5\u6570\u636e\u5e93\u5931\u8d25');\r\n   }else{\r\n       db.collection('xx').update({\"name\":'wilson001'},{$set:{ \"name\" : \"\u4fee\u6539\u540e\" }},function (err2, result) {\r\n          if(err2){\r\n\r\n          }else{\r\n             console.log(result)\r\n          }\r\n       });\r\n   }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u67e5<\/span>\uff1a<\/p>\n<pre>\/\/\u5728xx\u96c6\u5408\u4e2d\u67e5\u627eage=22\u7684\u90a3\u6761\u6570\u636e\r\nMongoClient.connect(DB_CONN_STR, function(err1, db) {\r\n   if(err1){\r\n      console.log('\u8fde\u63a5\u6570\u636e\u5e93\u5931\u8d25');\r\n   }else{\r\n      db.collection('xx').find({age:22}).toArray(function (err2,result) {\r\n         if(err2){\r\n            console.log('\u63d2\u5165\u6570\u636e\u5931\u8d25');\r\n         }else{\r\n            console.log(result);\r\n         }\r\n      });\r\n    }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u53ef\u76f4\u63a5\u6267\u884cmongodb\u8bed\u53e5<\/span>\uff08\u8fd9\u91cc\u8981\u6ce8\u610f\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u6240\u62e5\u6709\u7684\u6743\u9650\uff09<\/p>\n<pre>MongoClient.connect(DB_CONN_STR, function(err1, db) {\r\n   if(err1){\r\n      console.log('\u8fde\u63a5\u6570\u636e\u5e93\u5931\u8d25');\r\n   }else{\r\n      \/*-----------------------id\u81ea\u52a8\u9012\u589e\u51fd\u6570---------------------------------*\/\r\n        \/\/ \u8981\u4f7f\u7528\u6b64\u529f\u80fd\u65f6\uff0c\u8981\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u96c6\u5408,\u5982\u4e0b\r\n        \/\/ db.createCollection(\"counters\")\r\n        \/\/ db.counters.insert({_id:\"productid\",sequence_value:0})\r\n        var str=undefined;\r\n        str=function() {\r\n              function getNextSequenceValue(sequenceName){\r\n                var sequenceDocument = db.counters.findAndModify(\r\n                      {\r\n                        query:{_id: sequenceName },\r\n                        update: {$inc:{sequence_value:1}},\r\n                        new:true\r\n                });\r\n                return sequenceDocument.sequence_value;\r\n               }\r\n               db.xx22.insert({\r\n                  \"_id\":getNextSequenceValue(\"productid\"),\r\n                  \"product_name\":\"\u5185\u5bb9111\",\r\n                  \"category\":\"\u5185\u5bb9222\"\r\n               })\r\n         }+'';\r\n      \/*--------------------------------------------------------*\/\r\n     db.eval(str,function (err2,result) {\r\n         if(err2){\r\n             console.log('\u5931\u8d25');\r\n         }else{\r\n             console.log('\u6210\u529f');\r\n          }\r\n     })\r\n  }\r\n});\r\n\r\n\r\n\u63d0\u793a\uff1a\u5982\u679c\u8981\u62e5\u6709\u6267\u884ceval\u7684\u6743\u9650\uff0c\u5c31\u4e0d\u8981\u8bbe\u7f6e\u5bc6\u7801\uff0c\u8981\u4e48\u5c31\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a\r\n\u5728\u672a\u5f00\u542f\u5bc6\u7801\u9a8c\u8bc1\u7684\u65f6\u5019\u767b\u5f55mongo\u754c\u9762\uff0c\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\uff1a\r\nuse admin \r\ndb.createUser({user:\"root\",pwd:\"187\", roles:[{role:\"root\",db:\"admin\"}]})\r\ndb.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:['anyAction']}]}) \r\n\r\nuse xgllseo\r\ndb.createUser({user:'xgllseo',pwd:'123',roles:[{role:'sysadmin',db:'admin'}]}) \r\n\u4e4b\u540e\u5f00\u542fauth\u9a8c\u8bc1\r\n\u767b\u5f55\u6570\u636e\u5e93xgllseo\u5c31\u62e5\u6709\u6267\u884ceval\u6743\u9650\u4e86<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">auto\u9012\u589e\uff08\u4e00\u822c\u914d\u5408eval\u4f7f\u7528\uff09<\/span><\/p>\n<p>mongodb\u4e0d\u50cfmysql\u5176\u4ed6\u5173\u7cfb\u6570\u636e\u5e93\u4e00\u6837\uff0c\u6bcf\u589e\u52a0\u4e00\u6761\u6570\u636e\u540e\u5c31\u4f1a\u9012\u589e\u7d2f\u8ba1\u52a01\uff0c\u4ee5\u540e\u53ef\u4ee5\u9760id\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p>mongodb\u60f3\u6dfb\u52a0\u8fd9\u4e2a\u529f\u80fd\u6709\u70b9\u9ebb\u70e6\uff0c\u5982\u679c\u60f3\u4e3a\u54ea\u4e2a\u96c6\u5408\u6dfb\u52a0\u9012\u589e\u7684\u529f\u80fd\uff0c\u5c31\u8981\u4e3a\u6b64\u96c6\u5408\u989d\u5916\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e13\u95e8\u5b58\u50a8\u9012\u589e\u7684\u96c6\u5408\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5047\u5982\uff1a\u6709\u4e00\u4e2axx22\u7684\u96c6\u5408\u60f3\u4e3a\u8fd9\u4e2a\u96c6\u5408\u6dfb\u52a0\u81ea\u52a8\u9012\u589e\u7684\u529f\u80fd\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\uff0c\u5982\uff1a<\/p>\n<pre>1\uff0cdb.createCollection(\"\u96c6\u5408\u540d\")<\/pre>\n<pre>2\uff0cdb.\u96c6\u5408\u540d.insert({_id:\"productid\",sequence_value:0})<\/pre>\n<pre>3\uff0c\r\nfunction getNextSequenceValue(sequenceName){\r\n   var sequenceDocument = db.\u96c6\u5408\u540d.findAndModify(\r\n      {\r\n         query:{_id: sequenceName },\r\n         update: {$inc:{sequence_value:1}},\r\n         new:true\r\n       }\r\n    );\r\n   return sequenceDocument.sequence_value;\r\n}<\/pre>\n<pre>4\uff0c \/\/\u6d4b\u8bd5\uff0c\u4e0d\u505c\u7684\u5f80xx22\u4e2d\u6dfb\u52a0\u6570\u636e\uff0cxx22\u96c6\u5408\u7684_id\u5c31\u4e0d\u505c\u7684\u9012\u589e\u52a01\r\ndb.xx22.insert({\r\n  \"_id\":getNextSequenceValue(\"productid\"),\r\n  \"product_name\":\"\u5185\u5bb9111\",\r\n  \"category\":\"\u5185\u5bb9222\"\r\n})<\/pre>\n<p>\uff08\u4e3a\u4e86\u4e00\u4e2a\u96c6\u5408\u6dfb\u52a0\u9012\u589e\u529f\u80fd\u8fd8\u8981\u989d\u5916\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u611f\u89c9\u96c6\u5408\u6709\u70b9\u5197\u4f59\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\uff1a\u300a<a href=\"https:\/\/sdeno.com\/?p=5397\" target=\"_blank\">mongodb\u7b14\u8bb0\u7684\u5e38\u7528\u64cd\u4f5c<\/a>\u300b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB\u670d\u52a1\u5668\u7aef\u5b89\u88c5\u914d\u7f6e\u597d\u540e\uff0c\u5c31\u8981\u4e0e\u5ba2\u6237\u7aef\u4ea4\u4e92\uff0c\u4f8b\u5982php\u64cd\u4f5cmongodb\u3001express\u64cd\u4f5cmong [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,35],"tags":[],"class_list":["post-5404","post","type-post","status-publish","format-standard","hentry","category-mongodb","category-nodejs"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5404"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5404\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}