{"id":206209,"date":"2020-12-09T08:21:42","date_gmt":"2020-12-09T00:21:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=206209"},"modified":"2020-12-02T10:21:40","modified_gmt":"2020-12-02T02:21:40","slug":"mongodb-teach-you","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mongodb-teach-you.html","title":{"rendered":"MongoDB\u6570\u636e\u5e93\u624b\u628a\u624b\u6559\u4f60\u6765\u5b66\u4e60"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nMongoDB\u662f\u4e00\u4e2a\u4ecb\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u548c\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u4ea7\u54c1\uff0c\u662f\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5f53\u4e2d\u529f\u80fd\u6700\u4e30\u5bcc\uff0c\u6700\u50cf\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

MongoDB\u662f\u4e00\u4e2a\u4ecb\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u548c\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u4ea7\u54c1\uff0c\u662f\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5f53\u4e2d\u529f\u80fd\u6700\u4e30\u5bcc\uff0c\u6700\u50cf\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u3002<\/p>\n

Ubuntu\u5b89\u88c5MongoDB\uff0c\u76f4\u63a5sudo apt-get install mongodbMongoDB\u662f\u4e00\u4e2a\u4ecb\u4e8e\u5173\u7cfb\u6570\u636e\u5e93\u548c\u975e\u5173\u7cfb\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u4ea7\u54c1\uff0c\u662f\u975e\u5173\u7cfb\u6570\u636e\u5e93\u5f53\u4e2d\u529f\u80fd\u6700\u4e30\u5bcc\uff0c\u6700\u50cf\u5173\u7cfb\u6570\u636e\u5e93\u7684\u3002\u5b83\u652f\u6301\u7684\u6570\u636e\u7ed3\u6784\u975e\u5e38\u677e\u6563\uff0c\u662f\u7c7b\u4f3cjson\u7684bson\u683c\u5f0f\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b58\u50a8\u6bd4\u8f83\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n

Mongo\u6700\u5927\u7684\u7279\u70b9\u662f\u5b83\u652f\u6301\u7684\u67e5\u8be2\u8bed\u8a00\u975e\u5e38\u5f3a\u5927\uff0c\u5176\u8bed\u6cd5\u6709\u70b9\u7c7b\u4f3c\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u67e5\u8be2\u8bed\u8a00\uff0c\u51e0\u4e4e\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u5173\u7cfb\u6570\u636e\u5e93\u5355\u8868\u67e5\u8be2\u7684\u7edd\u5927\u90e8\u5206\u529f\u80fd\uff0c\u800c\u4e14\u8fd8\u652f\u6301\u5bf9\u6570\u636e\u5efa\u7acb\u7d22\u5f15\u3002<\/p>\n

\u5c06\u6570\u636e\u5b58\u50a8\u4e3a\u4e00\u4e2a\u6587\u6863\uff0c\u6587\u6863\u7c7b\u4f3c\u4e0eJson\u683c\u5f0f\uff0c\u6bd4\u5982\u4e0b\u9762\u5f62\u5f0f\u3002<\/p>\n

\r\n{ \r\n    name:\"\u6bdb\u5229\", \r\n    age:18\uff0c \r\n    address: {city:\"\u4e1c\u839e\"\uff0c country:\"china\"} \r\n} \r\n<\/pre>\n
MongoDB\u6570\u636e\u6a21\u578b<\/strong><\/div>\n

\"\"<\/p>\n

\u300c\u5982\u4f55\u8fdb\u5165\u548c\u9000\u51famongo\u300d<\/strong><\/div>\n

\"\"<\/p>\n

\u5e93\u7ea7\u64cd\u4f5c\u8bed\u53e5<\/strong><\/div>\n

\u663e\u793a\u6240\u6709\u5e93\uff1ashow dbs
\n\u5207\u6362\/\u521b\u5efa\u6570\u636e\u5e93\uff1ause \u6570\u636e\u5e93\u540d\u79f0
\n\u67e5\u770b\u6240\u5728\u5e93\uff1adb
\n\u5220\u9664\u5e93\uff1adb.dropDatabase()<\/p>\n

\u96c6\u5408\u64cd\u4f5c\u8bed\u53e5<\/strong><\/div>\n

\u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u7684\u96c6\u5408\uff1ashow collections
\n\u521b\u5efa\u96c6\u5408\uff1adb.createCollection(name)
\n\"\"
\n\u5220\u9664\u96c6\u5408\uff1adb.\u96c6\u5408\u540d\u79f0.drop()<\/p>\n

\u6587\u6863\u64cd\u4f5c<\/strong><\/div>\n

\u6dfb\u52a0\u6587\u6863(\u6570\u636e)<\/p>\n

db.\u96c6\u5408\u540d\u79f0.insert(document)<\/p>\n

\u6bcf\u4e00\u6761\u6570\u636e\uff0c\u5c31\u662f\u4e00\u4e2adocument\uff0c\u5c31\u662f\u4e00\u6761json\u4f8b\uff1adb.student.insert({name:'\u6bdb\u5229', age:18})<\/p>\n

\u6ce8\u610f\u70b9\uff1a<\/p>\n

\u6dfb\u52a0\u6587\u6863\u65f6\uff0c\u5982\u679c\u4e0d\u6307\u5b9a_id\u53c2\u6570 MongoDB\u4f1a\u4e3a\u6587\u6863\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684ObjectId<\/p>\n

\u7ed9\u5b9a _id\u4f8b\uff1adb.student.insert({'_id':1, name:'\u6bdb\u5229', age:18})<\/p>\n

\u6dfb\u52a0\u591a\u6761\u6587\u6863<\/p>\n

\r\ndb.student.insert([ \r\n    {name:'\u6bdb\u5229, sex:'\u7537', age:18}, \r\n    {name:\u2019\u6bdb\u5229\u7684\u7238\u7238', sex:'\u7537', age:47}, \r\n    {name:\u2019\u6bdb\u5229\u7684\u59d0\u59d0', sex:'\u5973', age:23}, \r\n    {name:\u2019\u6bdb\u5229\u7684\u5988\u5988\u2018, sex:\u2019\u5973', age:44}, \r\n]) \r\n<\/pre>\n
\u67e5\u8be2\u6587\u6863(\u6570\u636e)<\/strong><\/div>\n

db.\u96c6\u5408\u540d\u79f0.find([conditions])<\/p>\n

\u67e5\u770b\u96c6\u5408\u4e2d\u5168\u90e8\u6570\u636e\uff1adb.student.find()<\/p>\n

\u683c\u5f0f\u5316\u663e\u793a\uff1adb.student.find().pretty()<\/p>\n

\u67e5\u770b\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\uff1adb.student.find({name:'\u6bdb\u5229'})<\/p>\n

\u6761\u4ef6\u67e5\u8be2<\/strong><\/div>\n

and\u6761\u4ef6 {$and:[{expression1}, {expression1}, ...] }
\nor\u6761\u4ef6 {$or:[{expression1}, {expression1}, ...]
\n}db.student.find({$or:[{$and:[{sex:'\u5973'}, {age:23}]},{$and:[{sex:'\u7537'}, {age:{$gte:18}}]}]})
\n\"\"<\/p>\n

\u4fee\u6539\u6587\u6863(\u6570\u636e)<\/strong><\/div>\n

db.\u96c6\u5408\u540d\u79f0.update(, , {multi:})<\/p>\n

\u4fee\u6539\u4e00\u6761\u6570\u636e\uff1adb.student.update({sex:'\u7537'}, {age:20})\u628a\u8868\u4e2d\u7684\u7537\u7684age\u6539\u4e3a20<\/p>\n

\u6307\u5b9a\u5c5e\u6027\u4fee\u6539\uff1a{ $set: {age:20} }db.student.update({name:'\u6bdb\u5229'}, {$set: {age:666, sex: '\u4e0d\u544a\u8bc9\u4f60'}} )<\/p>\n

\u628a\u6bdb\u5229\u7684age\u6539\u4e3a666\uff0csex\u6539\u4e3a\u4e0d\u544a\u8bc9\u4f60<\/p>\n

\u66f4\u65b0\u96c6\u5408\u4e2d\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u6587\u6863\uff1a{ multi: true }<\/p>\n

\r\ndb.student.update({sex:'\u7537'}, {$set:{sex:'\u5973'}}, { multi:true} ) \r\n<\/pre>\n

\u628a\u6240\u6709\u6309\u7684\u6539\u4e3a\u5973\u7684<\/p>\n

\u5220\u9664\u6587\u6863(\u6570\u636e)<\/strong><\/div>\n

db.\u96c6\u5408\u540d\u79f0.remove(, {justOne:})<\/p>\n

\u5220\u9664\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6587\u6863\uff1adb.student.remove({})
\n\u5220\u9664\u96c6\u5408\u4e2d\u6ee1\u8db3\u6761\u4ef6\u7684\u6240\u6709\u6587\u6863db.student.remove({sex: '\u7537'})
\n\u53ea\u5220\u9664\u96c6\u5408\u4e2d\u6ee1\u8db3\u6761\u4ef6\u7684\u7b2c\u4e00\u6761\u6587\u6863\uff1a{ justOne: true }
\ndb.student.remove({sex:'\u7537'}, { justOne:true} )<\/p>\n

\u5728Python\u7a0b\u5e8f\u4e2d\u64cd\u4f5c<\/strong><\/div>\n

MongoDB\u5728 Python \u4e2d\u64cd\u4f5c MongoDB \uff0c\u9700\u8981\u4f7f\u7528 PyMongo \u5e93\uff0c\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1apip3 install pymongo\u3002<\/p>\n

\u8fde\u63a5\u65f6\u9700\u8981\u4f7f\u7528 PyMongo \u5e93\u91cc\u9762\u7684 MongoClient \u6a21\u5757\uff0c\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u521b\u5efa\u8fde\u63a5\uff0c\u9ed8\u8ba4\u53ea\u9700\u8981\u4f20\u5165IP\u548c\u7aef\u53e3\u53f7\u5373\u53ef\u3002\u5982\u679c\u6570\u636e\u5e93\u5b58\u5728\u8d26\u53f7\u5bc6\u7801\uff0c\u5219\u9700\u8981\u6307\u5b9a\u8fde\u63a5\u7684\u6570\u636e\u5e93\uff0cMongoDB\u7684\u5916\u7f51IP\u5fc5\u987b\u5f00\u653e\uff0c\u624d\u80fd\u8fde\u63a5\u6210\u529f\u3002<\/p>\n

\r\n>>> from pymongo import MongoClient \r\n>>> client = MongoClient('mongodb:\/\/192.168.92.92:27017')  \r\n>>> db = client.school \r\n>>> for student in db.students.find(): \r\n...     print('\u59d3\u540d:', student['name']) \r\n<\/pre>\n

\u672c\u6587\u5df2\u6536\u5f55 GitHub\uff1ahttps:\/\/github.com\/MaoliRUNsen\/runsenlearnpy100<\/p>\n","protected":false},"excerpt":{"rendered":"

MongoDB\u662f\u4e00\u4e2a\u4ecb\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u548c\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u4ea7\u54c1\uff0c\u662f\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5f53\u4e2d\u529f\u80fd\u6700\u4e30\u5bcc\uff0c\u6700\u50cf\u5173\u7cfb\u578b\u6570\u636e […]<\/p>\n","protected":false},"author":1898,"featured_media":206238,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[621],"class_list":["post-206209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-mongodb"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=206209"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206209\/revisions"}],"predecessor-version":[{"id":206220,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206209\/revisions\/206220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/206238"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=206209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=206209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=206209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}