Coder Social home page Coder Social logo

moncoll2's Introduction

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)

moncoll2's People

Contributors

atu4403 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.