{"id":1163,"date":"2019-05-26T10:17:40","date_gmt":"2019-05-26T01:17:40","guid":{"rendered":"https:\/\/lab4ict.com\/system\/?p=1163"},"modified":"2023-06-07T05:28:59","modified_gmt":"2023-06-06T20:28:59","slug":"python%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6mongodb%e3%81%ab%e3%82%a2%e3%82%af%e3%82%bb%e3%82%b9%ef%bc%88pymongo%ef%bc%89","status":"publish","type":"post","link":"https:\/\/lab4ict.com\/system\/archives\/1163","title":{"rendered":"Python\u3092\u4f7f\u7528\u3057\u3066MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\uff01\uff08PyMongo\uff09"},"content":{"rendered":"<p>Python\u3092\u4f7f\u7528\u3057\u3066\u3001MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<br \/>\n<!--more--><\/p>\n<h2>pymongo\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\uff01<\/h2>\n<p>pymongo\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; highlight: [1]; title: ; notranslate\" title=\"\">\r\n$ python -m pip install pymongo\r\nCollecting pymongo\r\n  Using cached https:\/\/files.pythonhosted.org\/packages\/11\/88\/dd1f8c4281a60791b043f55e338d0521049208f21e3de19ddc9c160dbbef\/pymongo-3.7.1-cp36-cp36m-manylinux1_x86_64.whl\r\nInstalling collected packages: pymongo\r\nSuccessfully installed pymongo-3.7.1\r\n<\/pre>\n<h2>pymongo\u3092\u4f7f\u7528\u3057\u3066MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\uff01<\/h2>\n<p>pymongo\u3092\u4f7f\u7528\u3057\u3066MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: plain; highlight: [1,5,6,7,8,9,10,11,12,13,14,15,16,18,20,21,27,33,34,36,42,43,44]; title: ; notranslate\" title=\"\">\r\n$ python\r\nPython 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) \r\n&#x5B;GCC 7.2.0] on linux\r\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\r\n&gt;&gt;&gt; from pymongo import MongoClient\r\n&gt;&gt;&gt; client = MongoClient()\r\n&gt;&gt;&gt; db = client.test_database\r\n&gt;&gt;&gt; collection = db.test_collection\r\n&gt;&gt;&gt; import datetime\r\n&gt;&gt;&gt; post = {&quot;author&quot;: &quot;Mike&quot;,\r\n...         &quot;text&quot;: &quot;My first blog post!&quot;,\r\n...         &quot;tags&quot;: &#x5B;&quot;mongodb&quot;, &quot;python&quot;, &quot;pymongo&quot;],\r\n...         &quot;date&quot;: datetime.datetime.utcnow()}\r\n&gt;&gt;&gt; posts = db.posts\r\n&gt;&gt;&gt; post_id = posts.insert_one(post).inserted_id\r\n&gt;&gt;&gt; post_id\r\nObjectId('5ba70fb1c9bb161d3c03bb4a')\r\n&gt;&gt;&gt; db.collection_names(include_system_collections=False)\r\n&#x5B;'posts']\r\n&gt;&gt;&gt; import pprint\r\n&gt;&gt;&gt; pprint.pprint(posts.find_one())\r\n{'_id': ObjectId('5ba70fb1c9bb161d3c03bb4a'),\r\n 'author': 'Mike',\r\n 'date': datetime.datetime(2018, 9, 23, 3, 58, 27, 70000),\r\n 'tags': &#x5B;'mongodb', 'python', 'pymongo'],\r\n 'text': 'My first blog post!'}\r\n&gt;&gt;&gt; pprint.pprint(posts.find_one({&quot;author&quot;: &quot;Mike&quot;}))\r\n{'_id': ObjectId('5ba70fb1c9bb161d3c03bb4a'),\r\n 'author': 'Mike',\r\n 'date': datetime.datetime(2018, 9, 23, 3, 58, 27, 70000),\r\n 'tags': &#x5B;'mongodb', 'python', 'pymongo'],\r\n 'text': 'My first blog post!'}\r\n&gt;&gt;&gt; posts.find_one({&quot;author&quot;: &quot;Eliot&quot;})\r\n&gt;&gt;&gt; post_id\r\nObjectId('5ba70fb1c9bb161d3c03bb4a')\r\n&gt;&gt;&gt; pprint.pprint(posts.find_one({&quot;_id&quot;: post_id}))\r\n{'_id': ObjectId('5ba70fb1c9bb161d3c03bb4a'),\r\n 'author': 'Mike',\r\n 'date': datetime.datetime(2018, 9, 23, 3, 58, 27, 70000),\r\n 'tags': &#x5B;'mongodb', 'python', 'pymongo'],\r\n 'text': 'My first blog post!'}\r\n&gt;&gt;&gt; post_id_as_str = str(post_id)\r\n&gt;&gt;&gt; posts.find_one({&quot;_id&quot;: post_id_as_str})\r\n&gt;&gt;&gt; quit()\r\n<\/pre>\n<h2>\u304a\u308f\u308a\u306b<\/h2>\n<p>Python\u304b\u3089MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u3001pymongo\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u305f\u3002<\/p>\n<h2>\u53c2\u8003\u60c5\u5831<\/h2>\n<ul>\n<li><a href=\"https:\/\/api.mongodb.com\/python\/\" rel=\"noopener noreferrer\" target=\"_blank\">pymongo<\/a><\/li>\n<li><a href=\"https:\/\/www.mongodb.com\/docs\/\" target=\"_blank\" rel=\"noopener\">MongoDB - Documentation<\/a><\/li>\n<li><a href=\"https:\/\/www.mongodb.com\/docs\/manual\/\" target=\"_blank\" rel=\"noopener\">MongoDB Server - Manual<\/a><\/li>\n<\/ul>\n<h2>\u95a2\u9023\u8a18\u4e8b<\/h2>\n<div class=\"sc_getpost\"><a class=\"clearfix\" href=\"https:\/\/lab4ict.com\/system\/archives\/9850\" ><div class=\"sc_getpost_thumb post-box-thumbnail__wrap\"><img decoding=\"async\" src=\"data:image\/gif;base64,R0lGODdhAQABAPAAAN3d3QAAACwAAAAAAQABAAACAkQBADs=\" width=\"150\" height=\"150\" alt=\"\u3010\u8a18\u4e8b\u4e00\u89a7\u3011MongoDB\u3092\u4f7f\u3044\u3053\u306a\u3059\uff01\" loading=\"lazy\" data-src=\"https:\/\/lab4ict.com\/system\/wp-content\/uploads\/2018\/09\/eyecatch_mongodb_01-150x150.png\" class=\"lazyload\"><\/div><div class=\"title\">\u3010\u8a18\u4e8b\u4e00\u89a7\u3011MongoDB\u3092\u4f7f\u3044\u3053\u306a\u3059\uff01<\/div><div class=\"substr\">MongoDB\u3092\u4f7f\u3044\u3053\u306a\u3059\u305f\u3081\u306e\u8a18\u4e8b\u4e00\u89a7\u3092\u63b2\u8f09\u3057\u307e\u3059\u3002...<\/div><\/a><\/div>\n<h2>\u53c2\u8003\u66f8\u7c4d\uff08Amazon\uff09<\/h2>\n<p><iframe sandbox=\"allow-popups allow-scripts allow-modals allow-forms allow-same-origin\" style=\"width:120px;height:240px;\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" src=\"\/\/rcm-fe.amazon-adsystem.com\/e\/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=infotecr-22&language=ja_JP&o=9&p=8&l=as4&m=amazon&f=ifr&ref=as_ss_li_til&asins=B07BGC4MJ1&linkId=0d0d3ef2181b83931f80739f4cfceb0e\"><\/iframe><\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3092\u4f7f\u7528\u3057\u3066\u3001MongoDB\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":9875,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[74,402,396],"class_list":["post-1163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-mongodb","tag-pymongo","tag-python"],"_links":{"self":[{"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":4,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"predecessor-version":[{"id":12234,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/posts\/1163\/revisions\/12234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/media\/9875"}],"wp:attachment":[{"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lab4ict.com\/system\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}