python3系を使ってのmongodbのを操作を行います。
まず必要なライブラリをインストールします。
1 2 3 |
# pip install pymongo |
以下が一通りの操作のサンプルです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# -*- coding: utf-8 -*- from pymongo import MongoClient client = MongoClient('localhost', 27017) # testデータベースを作成。無ければ作成。 db = client.test # menuコレクションを作成。無ければ作成。 collection = db.menu # 「collection.insert_one」ではDocumentを一つ作成 collection.insert_one({'name': 'カツ丼', 'category': 'ランチ', 'price': '900'}) # 「collection.insert_one」ではDocumentを複数作成 collection.insert_many([{'name': 'コース料理A', 'category': 'ディナー', 'price': '2000'}, {'name': 'Aセット', 'category': 'ランチ', 'price': '1000'}]) # 「collection.find」ではDocumentを複数取得 print("***** part1 *****") for menu in collection.find(): print(menu) print("***** part2 *****") # 「collection.find」では条件を指定してDocumentを取得することも可能 for menu in collection.find({'category': 'ランチ'}): print(menu) # 「collection.find_one」ではDocumentを一つ取得 print("***** part3 *****") print(collection.find_one()) # 「collection.find_one」では条件を指定してDocumentを取得することも可能 print("***** part4 *****") print(collection.find_one({'category': 'ディナー'})) # 「collection.update」でアップデートの実施 # $setを指定して、特定の要素だけを更新 collection.update({'name': 'Aセット'}, {'$set': {'kcal': "920"}}) print("***** part5 *****") print(collection.find_one({'name': 'Aセット'})) # 「collection.update」でアップデートの実施 # $setを指定しないと全てが書き換わる collection.update({'name': 'Aセット'}, {'kcal': "940"}) print("***** part6 *****") print(collection.find_one({'kcal': "940"})) # 「collection.update」で条件を指定してDocumentを削除 collection.remove({'name': 'Aセット'}) print("***** part7 *****") for menu in collection.find(): print(menu) # 「collection.update」で条件を指定しないとDocumentは全て削除される collection.remove() print("***** part8 *****") for menu in collection.find(): print(menu) # 「collection.drop」でコレクション自体の削除ができう collection.drop() print("***** part9 *****") for menu in collection.find(): print(menu) # 「drop_database」でデータベース自体の削除ができる client.drop_database('test') print("***** part10 *****") for menu in collection.find(): print(menu) |
こちらの実行結果は以下になります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ python sample.py ***** part1 ***** {'_id': ObjectId('5c186d97707b703dc068ec71'), 'name': 'カツ丼', 'category': 'ランチ', 'price': '900'} {'_id': ObjectId('5c186d97707b703dc068ec72'), 'name': 'コース料理A', 'category': 'ディナー', 'price': '2000'} {'_id': ObjectId('5c186d97707b703dc068ec73'), 'name': 'Aセット', 'category': 'ランチ', 'price': '1000'} ***** part2 ***** {'_id': ObjectId('5c186d97707b703dc068ec71'), 'name': 'カツ丼', 'category': 'ランチ', 'price': '900'} {'_id': ObjectId('5c186d97707b703dc068ec73'), 'name': 'Aセット', 'category': 'ランチ', 'price': '1000'} ***** part3 ***** {'_id': ObjectId('5c186d97707b703dc068ec71'), 'name': 'カツ丼', 'category': 'ランチ', 'price': '900'} ***** part4 ***** {'_id': ObjectId('5c186d97707b703dc068ec72'), 'name': 'コース料理A', 'category': 'ディナー', 'price': '2000'} ***** part5 ***** {'_id': ObjectId('5c186d97707b703dc068ec73'), 'name': 'Aセット', 'category': 'ランチ', 'price': '1000', 'kcal': '920'} ***** part6 ***** {'_id': ObjectId('5c186d97707b703dc068ec73'), 'kcal': '940'} ***** part7 ***** {'_id': ObjectId('5c186d97707b703dc068ec71'), 'name': 'カツ丼', 'category': 'ランチ', 'price': '900'} {'_id': ObjectId('5c186d97707b703dc068ec72'), 'name': 'コース料理A', 'category': 'ディナー', 'price': '2000'} {'_id': ObjectId('5c186d97707b703dc068ec73'), 'kcal': '940'} ***** part8 ***** ***** part9 ***** ***** part10 ***** |
このブログは株式会社CoLabMixによる技術ブログです。
GCP、AWSなどでのインフラ構築・運用や、クローリング・分析・検索などを主体とした開発を行なっています。
Ruby on RailsやDjango、Pythonなどの開発依頼などお気軽にお声がけください。
開発パートナーを増やしたいという企業と積極的に繋がっていきたいです。