PyMongo覚書
Pymongoの覚書になります、と言ってもmongoのクライアントから直接実行するのとほとんど同じように使うことができます。
モジュールインポート
import json import re import pymongo import bson from bson.son import SON from bson.code import Code from pymongo import MongoClient from bson.code import Code from datetime import datetime, timedelta
DB接続
事前に外部接続を許可する等して接続できるようにしておく必要があります。
mongo_client = MongoClient('ホスト名:27017') db=mongo_client['DB名']
コレクションの一覧確認
for c in db.collection_names(): print(c) || *** インサート >|python| # インサート db.book_collection.insert_one( { 'category': 'it', 'title': '入門 python3', 'synopsis': 'Pythonが誕生して四半世紀。データサイエンスやウェブ開発、セキュリティなどさまざまな分野でPythonの人気が急上昇中です。', 'publish': 'オライリー', 'authors': ['Bill Lubanovic', '斎藤康毅', '長尾高弘'], 'created': datetime.strptime("2015-12-01", "%Y-%m-%d") } )
以下の検索、集約では上でインサートしたフィールド情報を持ったコレクションを使って行います。
検索
# 条件なし検索 for c in db.book_collection.find().limit(5): print(c) # ○条件付き created > "2016-09-01" for c in db.book_collection.find({'created': {'$gt': datetime.strptime("2016-09-01", "%Y-%m-%d")}}).limit(5): print(c) # ◯複数条件 created > "2016-09-01" と category = 'fantasy' for c in db.book_collection.find({ 'created': {'$gt': datetime.strptime("2016-09-01", "%Y-%m-%d")}, 'category': 'fantasy' }).limit(5): print(c) #◯条件 authorsに田中太郎が含まれる for c in db.book_collection.find({'authors': {"$in":[u"田中太郎"]}}).limit(5): print(c) # ◯条件指定を変数に格納する created > "2016-10-01" condition={'created': {'$gt': datetime.strptime("2016-10-01", "%Y-%m-%d")}} for c in db.book_collection.find(condition).limit(5): print(c) # ソート # ◯createdで昇順にソート for c in db.book_collection.find({},{'title': 1, 'created': 1} ).sort('created', pymongo.DESCENDING): print(c)
集約
# 集約 # ◯publishで集約した件数を表示 for c in db.book_collection.aggregate([ { '$group' : {'_id': "$publish", 'count':{ '$sum': 1 } }} ]): print(c) # ◯createdの年、月、日で集約した件数を表示 for c in db.book_collection.aggregate([ { '$group' : {'_id': {'month': { '$month': '$created'}, 'day': { '$dayOfMonth': '$created' }, 'year': { '$year': "$created" }}, 'count':{ '$sum': 1 } }} ]): print(c) #◯publichとcreatedの年で集約した件数を表示 for c in db.book_collection.aggregate([ { '$group' : {'_id': {'publish': "$publish", 'year': { '$year': "$created" }}, 'count':{ '$sum': 1 } }} ]): print(c) # ◯createdの年、月、日で集約してソートした件数を表示 for c in db.book_collection.aggregate([ { '$group' : {'_id':{'year': { '$year': "$created" }, 'month': { '$month': "$created" } }, 'count':{ '$sum': 1 } }}, { '$sort': {'_id.month':1}} ]): print(c) # ◯createdの年、月、日で集約し件数上位3件を表示 for c in db.book_collection.aggregate([ { '$group' : {'_id':{'year': { '$year': "$created" }, 'month': { '$month': "$created" } },'count':{ '$sum': 1 } }}, { '$sort': {"count":-1}}, { '$limit': 3 } ]): print(c) # ◯authorに田中太郎が含まれるものをpublichで集約して表示 for c in db.book_collection.aggregate([ { '$match': {'authors': {"$in":['田中太郎']}}}, { '$group' : {'_id': "$publish", 'count':{ '$sum': 1 } }}, { '$sort': {"count":-1}} ]): print(c) # マップリデュースによるpublishごとの集約 mapper = Code(""" function () { emit(this.publish, 1); } """) reducer = Code(""" function (key, values) { var total = 0; for (var i = 0; i < values.length; i++) { total += values[i]; } return total; } """) result = db.book_collection.map_reduce(mapper, reducer, "myresults") for doc in result.find(): print(doc['_id'], ':', doc['value'])