Giter Club home page Giter Club logo

sanic_mongo's Introduction

sanic_mongo

sanic的mongodb异步工具,这个是基于sanic-mongo代码修改的。

用法:

from sanic import Sanic
from sanic_mongo import MongoClient

app = Sanic()

# 1.初始化mongodb客户端:
#   第一个参数为 app实例
#   从第二个参数开始,可以跟多个键值(Mongo数据库标识名=Mongodb_URI)
MongoClient.SetConfig(
    app,
    name_1="mongodb://127.0.0.1:27017/db1",
    db_name2="mongodb://root:[email protected]:27017/db_name2?authSource=admin"
)
MongoClient(app)
# 经过 MongoClient() 初始化后的 app 实例会生成一些属性:
#   app.config.MONGO_URIS 为一个字典 {"Mongo数据库标识名": "Mongodb_URI"}
#   app.MONGO_URIS = app.config.MONGO_URIS
#   app.extensions['SanicMongo'] 内容为 MongoClient实例化的对象
#   app.mongo 为一个字典 {'Mongo数据库标识名': MongoConnection客户端实例}


# 2.mongodb数据库的使用:
# 对于 app.mongo 中的每个MongoConnection都有以下属性
#   app.mongo['Mongo数据库标识名'].uri
#   app.mongo['Mongo数据库标识名'].client 这个是 AsyncIOMotorClient 创建的mongo客户端链接实例
#   app.mongo['Mongo数据库标识名'].db 这个是连上数据库时的db名称
# 所有的数据crud用法,都是使用motor的用法
#   https://motor.readthedocs.io/en/stable/api-asyncio/asyncio_motor_collection.html
cursor = app.mongo['name_1'].db.collection_name.find({'id': 123}).limit(50)
result_list = await cursor.to_list(length=50)

sanic_mongo's People

Contributors

garito avatar hsz1273327 avatar vastxiao 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.