{"id":21158,"date":"2023-06-09T22:40:15","date_gmt":"2023-06-09T13:40:15","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=21158"},"modified":"2023-06-10T09:57:09","modified_gmt":"2023-06-10T00:57:09","slug":"%e3%80%90mongodb%e3%80%91","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=21158","title":{"rendered":"\u3010NoSQL\u3011MongoDB\u306e\u57fa\u672c"},"content":{"rendered":"<h2>MongoDB\u3068\u306f\uff1f<\/h2>\n<p>NoSQL\u306e\u4e00\u7a2e\u3067\u3059\u3002NoSQL\u306b\u3082\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u304c\u305d\u306e\u4e2d\u3067\u3082\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8DB\u306b\u5206\u985e\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-21164\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2023\/06\/-2023-06-09-23.03.33-e1686319435293.png\" alt=\"\" width=\"500\" height=\"451\" \/><\/p>\n<h3>\u30b3\u30ec\u30af\u30b7\u30e7\u30f3<\/h3>\n<p>RDB\u3067\u8a00\u3048\u3070\u30c6\u30fc\u30d6\u30eb\u306b\u5f53\u305f\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/h3>\n<p>RDB\u3067\u8a00\u3048\u3070\u30ec\u30b3\u30fc\u30c9\u306b\u5f53\u305f\u308a\u307e\u3059\u3002JSON\u5f62\u5f0f\u3001Python\u3067\u8a00\u3046\u3068\u3053\u308d\u306e\u8f9e\u66f8\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h4>\u7279\u5fb4<\/h4>\n<p>\u4e8b\u524d\u306b\u5b9a\u7fa9\u304c\u4e0d\u8981\u3067\u305d\u308c\u305e\u308c\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3054\u3068\u306b\u7570\u306a\u308b\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h4>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u683c\u7d0d\u3067\u304d\u308b\u3082\u306e<\/h4>\n<ul>\n<li>\u6587\u5b57\u5217<\/li>\n<li>\u6570\u5024<\/li>\n<li>\u914d\u5217<\/li>\n<li>\u5225\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/li>\n<\/ul>\n<h2>\u30a8\u30c7\u30a3\u30b7\u30e7\u30f3<\/h2>\n<p>\u4ee5\u4e0b\u4e8c\u3064\u306e\u30a8\u30c7\u30a3\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u3002\u624b\u8efd\u306b\u4f7f\u3046\u306a\u3089\u30af\u30e9\u30a6\u30c9\u7248\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u4e0d\u8981\u306a\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u7248<\/li>\n<li>\u30af\u30e9\u30a6\u30c9\u7248<\/li>\n<\/ul>\n<h2>\u30af\u30e9\u30a6\u30c9\u7248\u5229\u7528\u306e\u6d41\u308c<\/h2>\n<h3>1.\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3002<\/h3>\n<p>https:\/\/www.mongodb.com\/ja-jp<\/p>\n<h3>2.\u30b5\u30a4\u30f3\u30a2\u30c3\u30d7(\u30e6\u30fc\u30b6\u30fc\u767b\u9332)\u3059\u308b\u3002<\/h3>\n<p>\u9069\u5f53\u306b\u5165\u529b\u3057\u307e\u3059\u3002<\/p>\n<h3>3.\u9069\u5f53\u306b\u5165\u529b\u3057\u3066\u9032\u3081\u308b\u3068\u30af\u30e9\u30b9\u30bf\u30fc\u304c\u4f5c\u6210\u6ce8\u306b\u306a\u308a\u307e\u3059\u3002<\/h3>\n<p>\u30af\u30e9\u30b9\u30bf\u30fc\u304c\u51fa\u6765\u4e0a\u304c\u308b\u307e\u306710\u5206\u307b\u3069\u304b\u304b\u308a\u307e\u3059\u306e\u3067\u3057\u3070\u3057\u304a\u5f85\u3061\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>4.\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u30a2\u30af\u30bb\u30b9\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h3>\n<p>\u300cDatabase Access\u300d\u2192\u300cAdd new Databaseuser\u300d<\/p>\n<ol>\n<li>\u300c\u30e6\u30fc\u30b6\u30fc\u540d\u300d\u3068\u300c\u30d1\u30b9\u30ef\u30fc\u30c9\u300d\u3092\u5165\u529b\u3059\u308b\u3002<\/li>\n<li>Role\u306f\u3068\u308a\u3042\u3048\u305a\u5168\u3066\u306eDB\u306b\u66f8\u304d\u8fbc\u307f\u8aad\u307f\u8fbc\u307f\u304c\u3067\u304d\u308b\u3088\u3046\u300cRead and wite to any database\u300d\u3092\u9078\u629e\u3059\u308c\u3070\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/li>\n<\/ol>\n<p>\u300cAdd User\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<h3>5.\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u8a2d\u5b9a<\/h3>\n<ol>\n<li>\u300cNetwork Access\u300d\u2192\u300cAdd IPAdress\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n<li>\u00a0\u3069\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u3082\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u300cAccess List Entry\u300d\u306b\u300c0.0.0.0\/0\u300d\u3092\u5165\u529b\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n<h3>6.\u30af\u30e9\u30b9\u30bf\u30fc\u3078\u306e\u63a5\u7d9a\u7528\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<ol>\n<li>Database\u3092\u9078\u629e<\/li>\n<li>\u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u30bf\u30fc\u306e\u300cConnect\u300d\u3092\u9078\u629e<\/li>\n<li>\u300cConnect to your application\u300d\u306e\u300cDrivers\u300d\u3092\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u8a00\u8a9e\u3068\u74b0\u5883\u3092\u9078\u629e\u3057\u307e\u3059\u3002\uff08Python\u3067\u3042\u308c\u3070\u300cPython\u300d\u3001\u300c3.6 or later\u300d\u3042\u305f\u308a\u3092\u9078\u629e\u3057\u3066\u304a\u3051\u3070\u826f\u3044\u3067\u3057\u3087\u3046\uff09<\/li>\n<li>\u300cAdd your connection string into your application code\u300d\u306b\u63a5\u7d9a\u7528\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002(mongodb+srv:\/\/xxx)<\/li>\n<li>\u4e0a\u8a18\u306e\u63a5\u7d9a\u7528\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u524d\u306e\u624b\u9806\u3067\u4f5c\u6210\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u540d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u57cb\u3081\u8fbc\u3093\u3067\u304a\u304d\u307e\u3059\u3002<\/li>\n<\/ol>\n<h2>Python\u304b\u3089MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3002<\/h2>\n<h3>\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">conda install pymongo dnspython -y\r\nconda install pillow<\/pre>\n<h2>MongoDB\u306b\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3059\u308b<\/h2>\n<pre class=\"lang:default decode:true\">class MongoAccess:\r\n    collection_name = '\u4f5c\u6210\u3057\u305f\u3044\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d'\r\n    def open(self):\r\n        self.client = pymongo.MongoClient('\u4e0a\u8a18\u624b\u9806\u3067\u4f5c\u6210\u3057\u305f\u63a5\u7d9a\u7528\u30d1\u30e9\u30e1\u30fc\u30bf\uff1amongodb+srv:\/\/xxxx')\r\n        self.db = self.client['XXXDB']\r\n\r\n    def close(self):\r\n        self.client.close()\r\n\r\n    def save(self, item):\r\n        self.db[self.collection_name].insert(dict(item))<\/pre>\n<h4>self.db = self.client['XXXDB']<\/h4>\n<p>\u300cXXXDB\u300d\u3068\u3044\u3046\u540d\u524d\u306eDB\u304cMongoDB\u4e0a\u306b\u306a\u3051\u308c\u3070\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002\u3042\u308c\u3070\u305d\u3053\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/p>\n<h3>self.client.close()<\/h3>\n<p>MongoDB\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u9589\u3058\u307e\u3059\u3002<\/p>\n<h3>self.db[self.collection_name].insert(dict(item))<\/h3>\n<p>\u8f9e\u66f8\u5f62\u5f0f\u3067\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n<h2>MongoDB\u304b\u3089\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u51fa\u3059\u3002<\/h2>\n<pre class=\"lang:default decode:true\">import pymongo\r\n\r\ncollection_name = '\u4efb\u610f\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d'\r\nclient = pymongo.MongoClient('mongodb+srv:\/\/xxx')\r\ndb = client['DB\u540d']\r\ncollection = db[collection_name]\r\nfor document in collection.find():\r\n    print(document)\r\nclient.close()<\/pre>\n<h3>for document in collection.find():<\/h3>\n<p>find\u306fPymongo\u306e\u69cb\u6587\u3067\u8f9e\u66f8\u578b\u3067MongoDB\u306b\u767b\u9332\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308c\u3070price\u3068\u3044\u3046\u30ad\u30fc\u304c3300\u306e\u3082\u306e\u3060\u3051\u3092\u62bd\u51fa\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">collection.find({'price': 3300})<\/pre>\n<p>\u4ed6\u306b\u3082pymongo\u306b\u306f\u3055\u307e\u3056\u307e\u306a\u30e1\u30bd\u30c3\u30c9\u3084\u6a5f\u80fd\u304c\u3042\u308b\u306e\u3067\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306a\u3069\u3092\u898b\u3066\u6709\u52b9\u6d3b\u7528\u3059\u308b\u3068\u826f\u3044\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"MongoDB\u3068\u306f\uff1f NoSQL\u306e\u4e00\u7a2e\u3067\u3059\u3002NoSQL\u306b\u3082\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u304c\u305d\u306e\u4e2d\u3067\u3082\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8DB\u306b\u5206\u985e\u3055\u308c\u307e\u3059\u3002 \u30b3\u30ec\u30af\u30b7\u30e7\u30f3 RDB\u3067\u8a00\u3048\u3070\u30c6\u30fc\u30d6\u30eb\u306b\u5f53\u305f\u308a\u307e\u3059\u3002 \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8 RDB\u3067\u8a00\u3048\u3070\u30ec\u30b3\u30fc\u30c9\u306b\u5f53\u305f\u308a\u307e\u3059\u3002 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[130,47],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21158"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21158"}],"version-history":[{"count":14,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21158\/revisions"}],"predecessor-version":[{"id":21174,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21158\/revisions\/21174"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}