Skip to main content

No project description provided

Project description

pymongoの単純なラッパー。接続情報、db_name、collection_nameをまとめてdictに含めることでDB操作のコードをシンプルにする。

Test

PyPI version

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


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)

Uploaded Source

Built Distribution

moncoll2-0.2.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page