MongoDB的轻量级pythonic或mapper。

mongobase的Python项目详细描述


MongoBase Logo

mongobase是一个python包,提供高级功能:

  • MongoDB的轻量级或映射器(ORM)
  • 具有自动类型检查的简单数据库模型结构定义
  • 从多个键生成高级自动文本搜索索引

依赖性

  • 皮蒙戈3.7+

有关Mongobase的更多信息

ComponentDescription
mongobasean high-level interface with model definition system from ModelBase and many database operations
modelbasean 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__。可以清楚地了解文档结构是如何的。 模型定义的其他组件是:

ComponentDescription
^{}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 ^{} is required. (required)
^{}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 ^{} property. (optional)
^{}^{}: value of ^{} is set as bigram strings. ^{}: the string in ^{} is parsed to morphemes (optional)
^{}^{}: each string has the same weight. ^{}: enable to set weights as ^{} (optional)
^{}indexes can be set. ^{} method creates the indexes on the db. (optional)

现在介绍基本用法。

插入和更新

>>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还有许多其他功能

如果您想了解其他功能,请查看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基本操作

许可证

Mongobase是麻省理工学院风格的许可证,可以在许可证文件中找到。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java什么会导致程序在它似乎拥有的监视器上被阻止?   java Android studio设置视图的背景色   java我可以保存一个文本文件而不给用户修改它的能力吗?   pdfbox PDFBOX2。0:java堆堆栈错误   java是维护和操作AllowList的有效方法   JAVAsql。SQLException:找不到适合jdbc的驱动程序:mysql://localhost:3306/asd性爱   如何使用java。lang.NullPointerException:void 安卓。支持v7。应用程序。ActionBar。setElevation(float)“”在空对象引用上'   java调试空指针异常   java正则表达式,以按令牌的特定匹配项拆分,同时忽略其他匹配项   java为JPanel设置边框上的笔划   并发@Schedule方法的java行为   如何在Java中使用泛型与语言运算符和泛型类扩展数   java Rhino Javascript如何为异常堆栈跟踪标记字符串源   运行可执行jar时发生java错误,无法找到或加载主类