No project description provided
Project description
pymongoの単純なラッパー。接続情報、db_name、collection_nameをまとめてdictに含めることでDB操作のコードをシンプルにする。
Install
pip install moncoll2
Useit
MC(settings: dict)-> pymongo.collection.Collection
settingsを元にしてcollectionを返す。with構文の中で操作をする事によってDBのcloseを確実に行う。
from moncoll2 import MC
settings = {
"username": "testuser",
"password": "testpass",
"host": "192.168.0.99",
"port": 12345,
"coll_name": "test_collenction",
"db_name": "test_db"
}
with MC(settings) as coll:
assert coll.full_name == 'test_db.test_collenction'
coll.insert_one({"a": 1})
res = coll.find_one()
assert res['a'] == 1
coll.drop()
Args
settings (dict): pymongoのMongoClientに渡す引数にdb_nameとcollection_nameを加えたもの
mongo_client – Tools for connecting to MongoDB — PyMongo 4.0.1 documentation
to_bulklist(_list: list, idname: str = "_id") -> list:
listをbulk_writeで使用できる形式にする
Args
_list (list): 元になるlist idname (str): _idになる項目名、元になるlistに含まれるなら省略可能
Returns
list: bulk_writeで使用できる形式のlist
Raises
BulkItemNotIdError: 元のlistに_id
という項目が含まれておらず、かつ引数idname
が指定されていない場合に発生
Examples:
>>> li = [
... {"name": "Karin", "gender": "female"},
... {"name": "Decker", "gender": "male"}
... ]
>>> res = to_bulklist(li, 'name')
>>> res[0]
UpdateOne({'_id': 'Karin'}, {'$set': {'name': 'Karin', 'gender': 'female', '_id': 'Karin'}}, True, None, None, None)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
moncoll2-0.2.0.tar.gz
(3.9 kB
view hashes)