開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
- MongoDB (NoSQL, データベース)
MongoDB and Python: Patterns and processes for the popular document-oriented database (Niall O'Higgins (著)、 O'Reilly Media)のChapter 2.(Reading and Writing to MongoDB with Python)の Updating Documents in a Collection を Python 2.7 ではなく Python 3.5 で取り組んでみる。(2.7と3では、pymongo module が少し変わったみたい。)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
import copy
def main():
try:
c = MongoClient(host="localhost", port=27017)
print('Connected successfully')
except ConnectionFailure as err:
print('Could not connect to MongoDB: {0}'.format(err), file=sys.stderr)
sys.exit(1)
dbh = c['mydb']
assert dbh.client == c
old_user_doc = dbh.users.find_one({'username': 'janedoe'})
print(old_user_doc)
print()
new_user_doc = copy.deepcopy(old_user_doc)
new_user_doc['email'] = 'janedoe74@example2.com'
dbh.users.replace_one({'username': 'janedoe'}, new_user_doc)
print(dbh.users.find_one({'username': 'janedoe'}))
print()
dbh.users.update_one({'username': 'janedoe'},
{'$set': {'email': 'janedoe74@example3.com', 'score': 1}})
print(dbh.users.find_one({'username': 'janedoe'}))
print()
dbh.users.update_many({'score': 0}, {'$set': {'flagged': False}})
print(dbh.users.find_one({'score': 0}))
print()
dbh.users.update_one({'score': 0}, {'$set': {'flagged': True}})
for user in dbh.users.find({'score': 0}):
print(user)
if __name__ == '__main__':
main()
入出力結果(Terminal, IPython)
$ ./sample3.py Connected successfully {'username': 'janedoe', '_id': ObjectId('57946c91a54d75162dc0c0d2'), 'surname': 'Doe', 'email': 'janedoe74@example3.com', 'firstname': 'Jane', 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'score': 1} {'username': 'janedoe', '_id': ObjectId('57946c91a54d75162dc0c0d2'), 'surname': 'Doe', 'email': 'janedoe74@example2.com', 'firstname': 'Jane', 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'score': 1} {'username': 'janedoe', '_id': ObjectId('57946c91a54d75162dc0c0d2'), 'surname': 'Doe', 'email': 'janedoe74@example3.com', 'firstname': 'Jane', 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'score': 1} {'username': 'janedoe', 'flagged': False, '_id': ObjectId('57946f2aa54d75164f103be7'), 'surname': 'Doe', 'email': 'janedoe74@example.com', 'score': 0, 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'firstname': 'Jane'} {'username': 'janedoe', 'flagged': True, '_id': ObjectId('57946f2aa54d75164f103be7'), 'surname': 'Doe', 'email': 'janedoe74@example.com', 'score': 0, 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'firstname': 'Jane'} {'username': 'janedoe', 'flagged': False, '_id': ObjectId('5794710ba54d7516754a7533'), 'surname': 'Doe', 'email': 'janedoe74@example.com', 'score': 0, 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'firstname': 'Jane'} {'username': 'janedoe', 'flagged': False, '_id': ObjectId('5798ab83a54d7503f87df302'), 'surname': 'Doe', 'email': 'janedoe74@example.com', 'firstname': 'Jane', 'dateofbirth': datetime.datetime(1974, 4, 12, 0, 0), 'score': 0} $
0 コメント:
コメントを投稿