🦍

Python(PyMongo)からMongoDBへアクセスしてみる

2021/11/20に公開

Python(PyMongo)からMongoDBへアクセスしてデータを取得してみました。

バージョン

  • MongoDB
4.2.9
  • python
3.8.9

テストデータ

  • db
gorira
  • collection
doubutsu
  • documents
rs0000:PRIMARY> db.doubutsu.find()
{ "_id" : ObjectId("61802fdf900569ec9f3b9109"), "name" : "gorira001", "age" : 20, "gender" : "male", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b910a"), "name" : "gorira002", "age" : 19, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b910b"), "name" : "gorira003", "age" : 18, "gender" : "male", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b910c"), "name" : "gorira004", "age" : 33, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b910d"), "name" : "gorira005", "age" : 11, "gender" : "male", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b910e"), "name" : "gorira006", "age" : 15, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b910f"), "name" : "gorira007", "age" : 12, "gender" : "male", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9110"), "name" : "gorira008", "age" : 9, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9111"), "name" : "gorira009", "age" : 19, "gender" : "female", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9112"), "name" : "gorira010", "age" : 38, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9113"), "name" : "gorira011", "age" : 3, "gender" : "male", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9114"), "name" : "gorira012", "age" : 10, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9115"), "name" : "gorira013", "age" : 21, "gender" : "female", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9116"), "name" : "gorira014", "age" : 19, "gender" : "male", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9117"), "name" : "gorira015", "age" : 35, "gender" : "female", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9118"), "name" : "gorira016", "age" : 19, "gender" : "female", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b9119"), "name" : "gorira017", "age" : 20, "gender" : "female", "country" : "ja" }
{ "_id" : ObjectId("61802fdf900569ec9f3b911a"), "name" : "gorira018", "age" : 29, "gender" : "female", "country" : "kp" }
{ "_id" : ObjectId("61802fdf900569ec9f3b911b"), "name" : "gorira019", "age" : 49, "gender" : "female", "country" : "ja" }
{ "_id" : ObjectId("61802fe0900569ec9f3b911c"), "name" : "gorira020", "age" : 29, "gender" : "female", "country" : "kp" }

Python から MongoDB へアクセス

PyMongoを使用してアクセスするため、pipで入れます。

python -m pip install pymongo

https://pymongo.readthedocs.io/en/stable/api/pymongo/index.html

アクセス

from pymongo import MongoClient

## MongoClientにアクセス情報を渡しておきます
client = MongoClient('mongodb://{ユーザ名}:{パスワード}@{MongoDBのIP}:{port}')

## db[gorira], collection[doubutsu]
col = client.gorira.doubutsu

client.close()

上記を実行してアクセスを確認してみます。

MongoDB側のログを見ると、ちゃんとアクセスが来ていました。

2021-11-20T10:09:43.715+0900 I  NETWORK  [conn20891] received client metadata from 192.168.99.5:50415 conn20891: { driver: { name: "PyMongo", version: "3.12.1" }, os: { type: "Windows", name: "Windows 10", architecture: "AMD64", version: "10.0.17134-SP0" }, platform: "CPython 3.8.9.final.0" }
2021-11-20T10:09:44.302+0900 I  NETWORK  [conn20891] end connection 192.168.99.5:50415 (37 connections now open)

DBからドキュメントを取得してみる

findでデータの取得も確認してみます。
※cousorオブジェクトとして返されます。すぐに結果を見たかったので、リスト内包でprintさせてます。

〜

documents = col.find()
[print(document) for document in documents]
client.close()
  • 結果

ちゃんと取得できました。

{'_id': ObjectId('61802fdf900569ec9f3b9109'), 'name': 'gorira001', 'age': 20.0, 'gender': 'male', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b910a'), 'name': 'gorira002', 'age': 19.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b910b'), 'name': 'gorira003', 'age': 18.0, 'gender': 'male', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b910c'), 'name': 'gorira004', 'age': 33.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b910d'), 'name': 'gorira005', 'age': 11.0, 'gender': 'male', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b910e'), 'name': 'gorira006', 'age': 15.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b910f'), 'name': 'gorira007', 'age': 12.0, 'gender': 'male', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b9110'), 'name': 'gorira008', 'age': 9.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b9111'), 'name': 'gorira009', 'age': 19.0, 'gender': 'female', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b9112'), 'name': 'gorira010', 'age': 38.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b9113'), 'name': 'gorira011', 'age': 3.0, 'gender': 'male', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b9114'), 'name': 'gorira012', 'age': 10.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b9115'), 'name': 'gorira013', 'age': 21.0, 'gender': 'female', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b9116'), 'name': 'gorira014', 'age': 19.0, 'gender': 'male', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b9117'), 'name': 'gorira015', 'age': 35.0, 'gender': 'female', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b9118'), 'name': 'gorira016', 'age': 19.0, 'gender': 'female', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b9119'), 'name': 'gorira017', 'age': 20.0, 'gender': 'female', 'country': 'ja'}
{'_id': ObjectId('61802fdf900569ec9f3b911a'), 'name': 'gorira018', 'age': 29.0, 'gender': 'female', 'country': 'kp'}
{'_id': ObjectId('61802fdf900569ec9f3b911b'), 'name': 'gorira019', 'age': 49.0, 'gender': 'female', 'country': 'ja'}
{'_id': ObjectId('61802fe0900569ec9f3b911c'), 'name': 'gorira020', 'age': 29.0, 'gender': 'female', 'country': 'kp'}

ついでにaggregateしてみる

  • gender でグループ化して雄ゴリラ・雌ゴリラの個体数をカウント
genders = col.aggregate([
    {'$group': {'_id': '$gender', 'count': {'$sum': 1}}}
])
[print(gender) for gender in genders]
client.close()

結果

{'_id': 'male', 'count': 12}
{'_id': 'female', 'count': 8}
  • 雄ゴリラ・雌ゴリラ別の年齢の「最大値・最小値・平均値」
gender_age_stats = col.aggregate([
    {
        '$group':
            {
                '_id': '$gender',
                'max': {'$max': '$age'},
                'min': {'$min': '$age'},
                'avg': {'$avg': '$age'}
            }
    }
])
[print(gender_age_stat) for gender_age_stat in gender_age_stats]
client.close()

結果

{'_id': 'male', 'max': 38.0, 'min': 3.0, 'avg': 17.25}
{'_id': 'female', 'max': 49.0, 'min': 19.0, 'avg': 27.625}
  • 上記をさらに国別で
gender_country_age_stats = col.aggregate([
    {
        '$group':
            {
                '_id': {'country': '$country', 'gender': '$gender'},
                'max': {'$max': '$age'},
                'min': {'$min': '$age'},
                'avg': {'$avg': '$age'}
            }
    }
])
[print(gender_country_age_stat) for gender_country_age_stat in gender_country_age_stats]
client.close()

結果

{'_id': {'country': 'ja', 'gender': 'female'}, 'max': 49.0, 'min': 19.0, 'avg': 28.8}
{'_id': {'country': 'kp', 'gender': 'male'}, 'max': 38.0, 'min': 9.0, 'avg': 20.428571428571427}
{'_id': {'country': 'ja', 'gender': 'male'}, 'max': 20.0, 'min': 3.0, 'avg': 12.8}
{'_id': {'country': 'kp', 'gender': 'female'}, 'max': 29.0, 'min': 19.0, 'avg': 25.666666666666668}

Discussion