Skip to main content

使用方便的实用程序和乐趣访问 mongodb

项目描述

小丑-mongodb

使用方便的实用程序和乐趣访问 mongodb。

使用 MongoInterface 连接到多个 mongo 服务器

例子:

GlobalInterface定义example/environ.py为:

from functools import cached_property

import volkanic
from joker.mongodb.interfaces import MongoInterface


class GlobalInterface(volkanic.GlobalInterface):
    package_name = 'example'
    default_config = {
        "mongoi": {
            "local": {},
            "remote": {
                "host": "192.168.22.122",
                "port": 27017
            }
        }
    }

    @cached_property
    def mongoi(self) -> MongoInterface:
        return MongoInterface.from_config(self.conf['mongoi'])

如果在以下位置之一找到配置文件:

  • 在开发环境中的项目目录下
  • ~/.example/config.json5
  • /etc/example/config.json5
  • /example/config.json5

它将覆盖GlobalInterface.default_config.

代码中的用法example/application.py

from bson import ObjectId
# noinspection PyUnresolvedReferences,PyPackageRequirements
from example.environ import GlobalInterface

gi = GlobalInterface()


def get_product(product_oid):
    coll = gi.mongoi.get_coll('remote', 'example', 'products')
    return coll.find_one({'_id': ObjectId(product_oid)})


if __name__ == '__main__':
    print(get_product('60f231605e0a4ea3c6c31c13'))

近期变动

版本 0.1.4

  • 添加MongoDocumentSchematorMongoFieldSchemator

0.1.3 版

  • 添加TransactionHelper

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

小丑-mongodb-0.1.4.tar.gz (27.0 kB 查看哈希)

已上传 source