跳转到主要内容

Muffin框架的MongoDB支持。

项目描述

松饼-Mongo – 为Muffin框架提供的MongoDB支持。

Tests Status PYPI Version Python Versions

需求

  • python >= 3.9

安装

应使用pip安装松饼-Mongo

pip install muffin-mongo

使用

设置插件并将其连接到您的应用

from muffin import Application
from muffin_mongo import Plugin as Mongo

# Create Muffin Application
app = Application('example')

# Initialize the plugin
# As alternative: db = DB(app, **options)
mongo = Mongo(db_url='mongodb://localhost:27017', database='db_name')
mongo.setup(app)

就是这样,现在您可以在视图中使用插件了

@app.route('/items', methods=['GET'])
async def get_items(request):
    """Return a JSON with items from the database."""
    documents = await mongo.items.find().sort('key').to_list(100)
    return [dict(dd.items(), _id=str(dd['_id'])) for dd in documents]

@app.route('/items', methods=['POST'])
async def insert_item(request):
    """Store items from JSON into database. Return ids."""
    data = await request.data()  # parse formdata/json from the request
    res = await mongo.items.insert_many(data)
    return [str(key) for key in res.inserted_ids]

配置选项

名称

默认值

描述

db_url

"mongodb://localhost:27017"

Mongo连接URL

database

None

数据库名称(可选)

在初始化插件时,您可以提供这些选项

mongo.setup(app, db_url='mongodb://localhost:27017')

或者从Muffin.Application配置中使用前缀MONGO_进行设置

MONGO_DB_URL = 'mongodb://localhost:27017'

Muffin.Application配置选项不区分大小写

错误跟踪器

如果您有任何建议、错误报告或不满,请向https://github.com/klen/muffin-mongo/issues的问题跟踪器报告

贡献

松饼-Mongo的开发发生在:https://github.com/klen/muffin-mongo

贡献者

  • klen (Kirill Klenov)

许可证

MIT许可证下授权。

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面