Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tsuBee5
Created July 5, 2018 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsuBee5/c51ddac112db577aa14dc1394e68349b to your computer and use it in GitHub Desktop.
Save tsuBee5/c51ddac112db577aa14dc1394e68349b to your computer and use it in GitHub Desktop.
Study-PyMongo
from pymongo import MongoClient
# MongoDB Parameters
dbuser = 'mongo_python'
dbpwd = 'mongo_python123'
dbname = 'mongo_123'
# Establishing a Connection
# client = MongoClient('localhost', 27017) # default
# client = MongoClient('mongodb://localhost:27017/') # the same as above
client = MongoClient('ds127841.mlab.com', 27841)
# Accessing Databases
db = client[dbname]
db.authenticate(dbuser, dbpwd)
# Inserting Single Document
book_data = {
'title': '10年後の仕事図鑑',
'content': 'AI、仮想通貨、モチベーション格差、46の仕事、働き方―。新たに始まる世界で、君はどう生きるか。',
'author' : ['堀江貴文', '落合陽一'],
'amazon_url': 'https://www.amazon.co.jp/1010年後の仕事図鑑-堀江-貴文/dp/4797394579'
}
collection = db.book_collection
collection.insert_one(book_data)
print('One book stored: {0}'.format(collection.inserted_id))
# Inserting Multiple Documents
book_1 = {
'title': '多動力',
'content': '堀江貴文のビジネス書の決定版!『多動力』',
'author' : '堀江貴文',
'amazon_url': 'https://www.amazon.co.jp/多動力-NewsPicks-Book-堀江貴文-ebook/dp/B072HVZ9RF/'
}
book_2 = {
'title': 'ゼロ なにもない自分に小さなイチを足していく',
'content': '堀江貴文はなぜ、逮捕され、すべてを失っても、希望を捨てないのか? ふたたび「ゼロ」となって、なにかを演じる必要もなくなった堀江氏がはじめて素直に、ありのままの心で語る、「働くこと」の意味と、そこから生まれる「希望」について。',
'author' : '堀江貴文',
'amazon_url': 'https://www.amazon.co.jp/ゼロ-なにもない自分に小さなイチを足していく-堀江-貴文-ebook/dp/B00G9KDQQU/'
}
book_3 = {
'title': '自分のことだけ考える。 無駄なものにふりまわされないメンタル術',
'content': '他人の目が気になる、人前に出ると緊張が止まらない、悪口を引きずってしまう、恥をかくのが怖い、モチベーションを持続できない…。こうした心の悩みを抱え、自分のやりたいことにブレーキをかけてしまっている人は多い。我慢せずに無駄なものを遠ざけ、心をフラットに生きる方法。メンタルコントロールの極意49。',
'author' : '堀江貴文',
'amazon_url': 'https://www.amazon.co.jp/146-自分のことだけ考える。-無駄なものにふりまわされないメンタル術-ポプラ新書/dp/B07C3MCR63'
}
new_result = collection.insert_many([book_1, book_2, book_3])
print('Multiple books stored: {0}'.format(new_result.inserted_ids))
# Retrieving Single Document
tadou = collection.find_one({'title': '多動力'})
print('Sigle Document'.center(30, '='))
print(tadou['_id'])
# for searching by '_id', ObjectId needs to be created
from bson.objectid import ObjectId
print(collection.find_one({'_id': ObjectId(tadou['_id'])}))
# Retrieving Multipel Documents
horiemon_books = collection.find({'author': '堀江貴文'})
print('Multipel Documents'.center(30, '='))
for book in horiemon_books:
print(book['title'] + '\n' + book['amazon_url'])
# Updating Document
collection.find_one_and_update(
{'title': '多動力'}, {'$set': {'title': '多動力 (NewsPicks Book)'}})
print('Document Updated'.center(30, '='))
print(collection.find_one({'title': '多動力'})) # =>None
# Deleting Document
collection.delete_one({'title': '10年後の仕事図鑑'})
print('Document Deleted'.center(30, '='))
print(collection.find_one({'title': '10年後の仕事図鑑'})) # =>None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment