{"rsdb":{"rid":"242533","subhead":"","postdate":"0","aid":"166991","fid":"57","uid":"1","topic":"1","content":"
\n

MongoDB \u6982\u5ff5\u7406\u89e3<\/h3> \n

\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\"<\/p> \n

MongoDB shell \u8fde\u63a5 Mongodb \u670d\u52a1<\/strong><\/p> \n

\u6807\u51c6 URI \u8fde\u63a5\u8bed\u6cd5\uff1a<\/p> \n

\r\nmongodb:\/\/[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][\/[database][?options]]\r\n\r\nmongodb:\/\/ \u8fd9\u662f\u56fa\u5b9a\u7684\u683c\u5f0f\uff0c\u5fc5\u987b\u8981\u6307\u5b9a\u3002\r\nusername:password@ \u53ef\u9009\u9879\uff0c\u5982\u679c\u8bbe\u7f6e\uff0c\u5728\u8fde\u63a5\u6570\u636e\u5e93<\/a>\u670d\u52a1\u5668\u4e4b\u540e\uff0c\u9a71\u52a8\u90fd\u4f1a\u5c1d\u8bd5\u767b\u9646\u8fd9\u4e2a\u6570\u636e\u5e93\r\nhost1 \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\u3002\r\nportX \u53ef\u9009\u7684\u6307\u5b9a\u7aef\u53e3\uff0c\u5982\u679c\u4e0d\u586b\uff0c\u9ed8\u8ba4\u4e3a27017\r\n\/database \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\r\n?options \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&\u6216;\uff08\u5206\u53f7\uff09\u9694\u5f00\r\n<\/pre> \n 

\u201cshow dbs\u201d \u547d\u4ee4\u53ef\u4ee5\u663e\u793a\u6240\u6709\u6570\u636e\u7684\u5217\u8868\u3002<\/p> \n

\u6267\u884c \u201cdb\u201d \u547d\u4ee4\u53ef\u4ee5\u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u5bf9\u8c61\u6216\u96c6\u5408\u3002<\/p> \n

\u8fd0\u884c\u201duse\u201d\u547d\u4ee4\uff0c\u53ef\u4ee5\u8fde\u63a5\u5230\u4e00\u4e2a\u6307\u5b9a\u7684\u6570\u636e\u5e93\u6216\u8005\u521b\u5efa\u6570\u636e\u5e93\uff1a<\/p> \n

\r\nuse DATABASE_NAME\r\n<\/pre> \n 

\u521a\u521b\u5efa\u7684\u6570\u636e\u5e93 \u5e76\u4e0d\u5728\u6570\u636e\u5e93\u7684\u5217\u8868\uff08show dbs\uff09\u4e2d\uff0c \u8981\u663e\u793a\u5b83\uff0c\u6211\u4eec\u9700\u8981\u5411\u6570\u636e\u5e93\u63d2\u5165\u4e00\u4e9b\u6570\u636e\u3002<\/p> \n

MongoDB \u5220\u9664\u6570\u636e\u5e93\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/strong><\/p> \n

\r\ndb.dropDatabase()\r\n<\/pre> \n 

\u5220\u9664\u96c6\u5408<\/strong><\/p> \n

\r\n\u96c6\u5408\u5220\u9664\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a\r\ndb.collection.drop()\r\n<\/pre> \n 

MongoDB \u4f7f\u7528 insert() \u6216 save() \u65b9\u6cd5\u5411\u96c6\u5408\u4e2d\u63d2\u5165\u6587\u6863\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p> \n

\r\ndb.COLLECTION_NAME.insert(document)\r\n<\/pre> \n 

\u6587\u6863document\u7684\u6570\u636e\u7ed3\u6784\u548cJSON\u57fa\u672c\u4e00\u6837\u3002<\/p> \n

\u6240\u6709\u5b58\u50a8\u5728\u96c6\u5408\u4e2d\u7684\u6570\u636e\u90fd\u662fBSON\u683c\u5f0f\u3002<\/p> \n

BSON\u662f\u4e00\u79cd\u7c7bjson\u7684\u4e00\u79cd\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u5b58\u50a8\u683c\u5f0f,\u7b80\u79f0Binary JSON\u3002<\/p> \n

\u67e5\u770b\u5df2\u63d2\u5165\u6587\u6863\uff1a<\/strong><\/p> \n

\r\ndb.col.find()\r\n#\u5982\u679c\u60f3\u8981\u4ee5\u8bfb\u53cb\u597d\u7684\u683c\u5f0f\u663e\u793a\u5219\u683c\u5f0f\u5982\u4e0b\uff1a\r\ndb.col.find().pretty()\r\n<\/pre> \n 

MongoDB \u66f4\u65b0\u6587\u6863<\/strong><\/p> \n

\r\nMongoDB \u4f7f\u7528 update() \u548c save() \u65b9\u6cd5\u6765\u66f4\u65b0\u96c6\u5408\u4e2d\u7684\u6587\u6863\u3002\u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u8be6\u7ec6\u6765\u770b\u4e0b\u4e24\u4e2a\u51fd\u6570\u7684\u5e94\u7528\u53ca\u5176\u533a\u522b\u3002\r\n<\/pre> \n 

update() \u65b9\u6cd5
update() \u65b9\u6cd5\u7528\u4e8e\u66f4\u65b0\u5df2\u5b58\u5728\u7684\u6587\u6863\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p> \n

\r\n    db.collection.update(\r\n       \n  \n   ,\r\n       \n   \n    , { upsert: \n    \n     , multi: \n     \n      , writeConcern: \n      \n        } ) \n      <\/document>\n     <\/boolean>\n    <\/boolean>\n   <\/update>\n  <\/query><\/pre> \n 

\u53c2\u6570\u8bf4\u660e\uff1a<\/p> \n

\r\nquery : update\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u7c7b\u4f3csql update\u67e5\u8be2\u5185where\u540e\u9762\u7684\u3002\r\nupdate : update\u7684\u5bf9\u8c61\u548c\u4e00\u4e9b\u66f4\u65b0\u7684\u64cd\u4f5c\u7b26\uff08\u5982$,$inc...\uff09\u7b49\uff0c\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3asql update\u67e5\u8be2\u5185set\u540e\u9762\u7684\r\nupsert : \u53ef\u9009\uff0c\u8fd9\u4e2a\u53c2\u6570\u7684\u610f\u601d\u662f\uff0c\u5982\u679c\u4e0d\u5b58\u5728update\u7684\u8bb0\u5f55\uff0c\u662f\u5426\u63d2\u5165objNew,true\u4e3a\u63d2\u5165\uff0c\u9ed8\u8ba4\u662ffalse\uff0c\u4e0d\u63d2\u5165\u3002\r\nmulti : \u53ef\u9009\uff0cmongodb \u9ed8\u8ba4\u662ffalse,\u53ea\u66f4\u65b0\u627e\u5230\u7684\u7b2c\u4e00\u6761\u8bb0\u5f55\uff0c\u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u4e3atrue,\u5c31\u628a\u6309\u6761\u4ef6\u67e5\u51fa\u6765\u591a\u6761\u8bb0\u5f55\u5168\u90e8\u66f4\u65b0\u3002\r\nwriteConcern :\u53ef\u9009\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\u3002\r\n<\/pre> \n 

save() \u65b9\u6cd5<\/strong><\/p> \n

save() \u65b9\u6cd5\u901a\u8fc7\u4f20\u5165\u7684\u6587\u6863\u6765\u66ff\u6362\u5df2\u6709\u6587\u6863\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p> \n

\r\ndb.collection.save(\r\n   \n  \n   ,\r\n   {\r\n     writeConcern: \n   \n     } ) \n   <\/document>\n  <\/document><\/pre> \n 

\u53c2\u6570\u8bf4\u660e\uff1a<\/p> \n

\r\ndocument : \u6587\u6863\u6570\u636e\u3002\r\nwriteConcern :\u53ef\u9009\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\u3002\r\n<\/pre> \n 

\u5b98\u65b9\u63a8\u8350\u4f7f\u7528 deleteOne() \u548c deleteMany() \u65b9\u6cd5\u3002<\/p> \n

\u5982\u5220\u9664\u96c6\u5408\u4e0b\u5168\u90e8\u6587\u6863\uff1a<\/p> \n

\r\ndb.COLLECTION_NAME.deleteMany({})\r\n<\/pre> \n 

\u5220\u9664 status \u7b49\u4e8e A \u7684\u5168\u90e8\u6587\u6863\uff1a<\/p> \n

\r\ndb.COLLECTION_NAME.deleteMany({ status : "A" })\r\n<\/pre> \n 

\u5220\u9664 status \u7b49\u4e8e D \u7684\u4e00\u4e2a\u6587\u6863\uff1a<\/p> \n

\r\ndb.COLLECTION_NAME.deleteOne( { status: "D" } )\r\n<\/pre> \n<\/div>","orderid":"0","title":"MongoDB \u6982\u5ff5\u7406\u89e3\u3001MongoDB shell \u8fde\u63a5 Mongodb \u670d\u52a1","smalltitle":"","mid":"0","fname":"\u6570\u636e\u5e93\u7f16\u7a0b","special_id":"0","bak_id":"0","info":"0","hits":"194","pages":"1","comments":"0","posttime":"2017-12-14 14:32:29","list":"1513233149","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"MongoDB<\/A> \u6982\u5ff5<\/A> \u7406\u89e3<\/A> shell<\/A> \u8fde\u63a5<\/A> Mongodb<\/A> \u670d\u52a1<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.110.149.170","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"MongoDB \u6982\u5ff5\u7406\u89e3\u3001MongoDB shell \u8fde\u63a5 Mongodb \u670d\u52a1","lastview":"1713505601","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}