MongoDB的轻量级pythonic或mapper。
mongobase的Python项目详细描述
mongobase是一个python包,提供高级功能:
- MongoDB的轻量级或映射器(ORM)
- 具有自动类型检查的简单数据库模型结构定义
- 从多个键生成高级自动文本搜索索引
依赖性
- 皮蒙戈3.7+
有关Mongobase的更多信息
Component | Description |
---|---|
mongobase | an high-level interface with model definition system from ModelBase and many database operations |
modelbase | an OR Mapper class with automatic type checking according to the defined structure (MongoBase subclass) |
蒙哥巴的哲学是
- 允许在python上轻松、安全地使用mongodb
- 只需快速查看模型定义,就可以清晰地查看数据模型的所有内容
- 易于学习使用。例如,方法名对应于mongodb,以便能够像在mongoclient上一样使用它们。
- 高性能。它使用最新的连接池机制,以便有效地使用客户端对象
基本接口
模型定义
这是模型的示例定义。
classBird(MongoBase):__collection__='birds'__structure__={'_id':ObjectId,'name':str,'age':int,'is_able_to_fly':bool,'created':dt.datetime,'updated':dt.datetime}__required_fields__=['_id','name']__default_values__={'_id':ObjectId(),'is_able_to_fly':False,'created':dt.datetime.now(dt.timezone.utc),'updated':dt.datetime.now(dt.timezone.utc)}__validators__={'name':validate_length(0,1000),}__search_text_keys__=['name']__search_text_index_type__='bigram'__indexes__=[[('item_name',ASCENDING),],]
核心模型结构被一个词定义为__structure__
。可以清楚地了解文档结构是如何的。
模型定义的其他组件是:
Component | Description |
---|---|
^{ | the collection name of the document. (required) |
^{ | the core definition of the model. the type is automatically checked everytime when it is written on the db. the key ^{ |
^{ | required properties. (optional) |
^{ | set default values for properties. (optional) |
^{ | validator methods automatically check the value when the document is written on the db. (optional) |
^{ | multiple keys can be set for the search text index. automatically written as the ^{ |
^{ | ^{ |
^{ | ^{ |
^{ | indexes can be set. ^{ |
现在介绍基本用法。
插入和更新
>>chicken=Bird({'_id':ObjectId(),'name':'chicken','age':3})>>chicken.save(){'_id':ObjectId('5c80f4fa16fa0d6c102cd2a6'),'name':'chicken','age':3,'is_able_to_fly':False,'created':datetime.datetime(2019,3,7,10,39,54,643685,tzinfo=datetime.timezone.utc),'updated':datetime.datetime(2019,3,7,10,39,54,643690,tzinfo=datetime.timezone.utc)}>>chicken.is_able_to_fly=True>>chicken.update()
查找
>>>Bird.findOne({'name':'mother chicken'}){'_id':ObjectId('5c79166716fa0d215968d3ba'),'name':'mother chicken','age':63,'is_able_to_fly':False,'created':datetime.datetime(2019,3,1,11,20,21,306000),'updated':datetime.datetime(2019,3,1,11,20,21,306000)}>>>mother_chicken.remove()1>>>all_chickens=Bird.find({'name':'chicken'},sort=[('_id',ASCENDING)])listofmongobaseinstancesarereturned.>>>len(all_chickens)18>>>Bird.count()201
批量操作
- 批量插入
>>>many_pigeon=[]>>>foriinrange(10000):>>>many_pigeon+=[Bird({'_id':ObjectId(),'name':f'pigeon','age':i})]>>>Bird.bulk_insert(many_pigeon)10000
- 批量更新
>>> updates = []
>>> for pigeon in many_pigeon:
>>> pigeon.age *= 3
>>> updates += [pigeon]
>>> Bird.bulk_update(updates)
10000
上下文数据库
withdb_context(db_uri='localhost',db_name='test')asdb:flamingo=Bird({'_id':ObjectId(),'name':'flamingo','age':20})flamingo.save(db=db)flamingo.age=23flamingo=flamingo.update(db=db)flamingo=Bird.findAndUpdateById(flamingo._id,{'age':24},db=db)n_flamingo=Bird.count({'name':'flamingo'},db=db)Bird.count({'name':'flamingo'})
多处理
defbreed(tasks):db=Bird._db()# create a MongoDB Client for the forked processforiinrange(len(tasks)):sparrow=Bird({'_id':ObjectId(),'name':f'sparrow','age':0})sparrow.save(db=db)tasks=[[f'task {i}'foriinrange(N_BATCH)]forjinrange(N_PROCESS)process_pool=multiprocessing.Pool(N_PROCESS)process_pool.map(breed,tasks)
Mongobase还有许多其他功能
>>> updates = []
>>> for pigeon in many_pigeon:
>>> pigeon.age *= 3
>>> updates += [pigeon]
>>> Bird.bulk_update(updates)
10000
如果您想了解其他功能,请查看mongobase.py文件。
数据库设置
只需写入mongobase/config.py
MONGO_DB_URI="101.21.434.121"MONGO_DB_URI_TEST="localhost"MONGO_DB_NAME="zoo"MONGO_DB_NAME_TEST="zoo-test"MONGO_DB_CONNECT_TIMEOUT_MS=3000MONGO_DB_SERVER_SELECTION_TIMEOUT_MS=3000MONGO_DB_SOCKET_TIMEOUT_MS=300000MONGO_DB_SOCKET_KEEP_ALIVE=TrueMONGO_DB_MAX_IDLE_TIME_MS=40000MONGO_DB_MAX_POOL_SIZE=200MONGO_DB_MIN_POOL_SIZE=10MONGO_DB_WAIT_QUEUE_MULTIPLE=12MONGO_DB_WAIT_QUEUE_TIMEOUT_MS=100
开始
如果您开始使用Mongobase,这里有一个教程Jupyter笔记本。
强烈建议检查一下。
https://github.com/kazukiotsuka/mongobase/blob/master/tutorial/MongoBase_starting_guide.ipynb
发布和贡献
许多方法都是pymongo的包装器。
这个库包含许多功能。
如果你能随时补充他们的方法,我将不胜感激。
版本0.3.0
新功能
- bulk_insert()
- 批量更新()
- 使用connectionpool提高性能(每个进程一个mongoclient)
- mongobase_start_guide.ipynb
- 上下文数据库客户端模式由
with db_context() as db
- 提高代码效率
- 取消了
insert_if_not_exists
的参数save(), update()
- 更改了一些方法名(例如删除->;删除)
- 使用pymongo>;3.5方法(例如insert_one())
- 增强文档
版本0.2.0
新功能
- Mongobase和ModelBase类是分开的
- 启用动态使用mongoclient实例
- 添加了一些有用的MongoDB操作
版本0.1.0
新功能
- 初始实现
- 自动类型检查机制
- MongoDB基本操作
许可证
with db_context() as db
insert_if_not_exists
的参数save(), update()
- Mongobase和ModelBase类是分开的
- 启用动态使用mongoclient实例
- 添加了一些有用的MongoDB操作
版本0.1.0
新功能
- 初始实现
- 自动类型检查机制
- MongoDB基本操作
许可证
Mongobase是麻省理工学院风格的许可证,可以在许可证文件中找到。