MongoDB ORM for Python – 完整指南
您是一名 Python 开发人员,正在为您的惊人应用程序寻找适用于 Python 的 MongoDB ORM 吗?那么本教程就是为你准备的!
MongoDB 的受欢迎程度随着时间的推移而增长,尤其是作为一个强大的数据库管理系统。不可否认——MongoDB 有时对初学者来说是一个挑战。除了文档的复杂性之外,这些组件还有许多缺点。
既然您已经决定将 MongoDB 与 Python 一起使用,那么您肯定需要一种称为 ORM 或 ODM 的东西。ORM 或对象关系映射器也称为 ODM 或对象文档映射器,用于轻松地与编程语言或其框架和 NoSQL 数据库一起使用,并且速度更快。
Python 作为一种古老而强大的编程语言,当与强大的 NoSQL 数据库服务(如 MongoDB)捆绑时,可以生成更强大的应用程序。为了让生活更轻松,引入了 ODM。大多数用于 Python 的 MongoDB ORM 解决了相同的问题,但由于各种原因它们有所不同。它们还具有不同级别的范围和复杂性。
在本指南中,我将为您介绍适用于 Python 的顶级 MongoDB ORM,以便您可以从最适合您的应用程序的一个中进行选择。现在让我们从这个内容丰富的指南开始吧!
什么是对象关系映射器 (ORM)?
在 MongoDB 等非 SQL 数据库中,文档映射器或对象文档映射器将对象映射到基于文档的数据库。开发人员可以使用对象文档映射器轻松为集合内的文档定义模式,以便能够存储数据。这简化了理解和管理数据的过程。
这允许个人很好地对集合中的文档进行分类,从而改善数据表示。使用 ODM,用户还可以轻松地将新属性和字段添加到现有文档中,而无需修改任何其他内容。
当与 Python 或其任何框架(如 Django 或 Flask)一起使用时,我们使用 MongoDB ORM for Python。我将通过列出一些适用于 Python 的顶级 MongoDB ORM 来帮助您选择最好的。
用于 Python 的顶级 MongoDB ORM
那么,让我们开始使用 Python 列表的顶级 MongoDB ORM
1. PyMongo
The PyMongo MongoDB ORM for Python is a native and highly popular Python driver for MongoDB. It supports MongoDB versions MongoDB 2.6, 3.0, 3.2, 3.4, 3.6, 4.0, 4.2, 4.4, and 5.0. The package is essentially built to provide Python developers with all the right tools to interact with a MongoDB database.
The PyMongo package was authored by Mike Dierolf and is maintained by Bernie Hackett.
Installation
- Install the package from PyPi with PIP using the below command:
| 1 |
$ python -m pip install pymongo
| | —- | —- |
- For an easy_install using setuptools, you can pass the below command:
| 1 |
$ python -m easy_install pymongo
| | —- | —- |
- Or, if you have downloaded the project source, you can simply run the below command:
| 1 |
$ python setup.py install
| | —- | —- |
For more information on the PyMongo MongoDB ORM for Python, visit the official docs of this package here.
2. Mongokit
Using MongoKit as a gateway to the great PyMongo driver adds a schema layer and validation. This MongoDB ORM for Python has gained immense popularity across many Python-MongoDB users. The KISS and DRY principles have been followed throughout, making this document as simple and light as possible.
It was aimed to be made as simple, faster, and powerful as possible by the authors even with the inclusion of all those features a MongoDB ORM for Python should have. Some of them are, random query support, polymorphism and inheritance, custom validators, untyped field support, and the list goes on.
Installation
- Install the package from PyPi with PIP using the below command:
| 1 |
$ pip install mongokit
| | —- | —- |
For more information on the Mongokit MongoDB ORM for Python, visit the official docs of this package here.
3. Motor
The Motor ODM is yet another popular Python driver for MongoDB. Utilizing the full advantage of modern hardware, Motor can process thousands of requests per second. It offers users with co-routine-based APIs for non-blocking access to MongoDB.
It is specially designed to work with Tornado and Asyncio applications in Python. This MongoDB ORM for Python is authored by A. Jesse Jiryu Davis.
Installation
- Install the package from PyPi with PIP using the below command:
| 1 |
$ pip install motor
| | —- | —- |
For more information on the Motor MongoDB ORM for Python, visit the official docs of this package here.
4. Beanie
Beanie is a powerful and asynchronous ODM for MongoDB that is based on Motor and Pydantic. Beanie makes it easier for Python developers to add, delete, update, or read data from a collection inside a document.
It supports out-of-the-box migration systems of data and schema. This ODM is authored by a user named Roman Right on GitHub.
Installation
- Install the package from PyPi with PIP using the below command:
| 1 |
$ pip install beanie
| | —- | —- |
- Install the package from Poetry using the below command:
| 1 |
$ poetry add beanie
| | —- | —- |
For more information on the Beanie MongoDB ORM for Python, visit the official docs of this package here.
5. Minimongo
Minimongo is designed with the aim to offer Python developers with a lightweight, schemaless, minimal, and a Pythonic object-oriented model management for MongoDB. You can make this ODM work with an existing schema or even from third-party Python apps.
This package is authored by Steve Lucy.
Installation
- Install the package from setuptools:
| 1 |
$ easy_install -U minimongo
| | —- | —- |
- Install the package from GitHub and run command:
| 1 |
$ python setup.py install
| | —- | —- |
For more information on the Minimongo MongoDB ORM for Python, visit the official docs of this package here.
6. MongoEngine
Lastly, I would like to cover the MongoEngine MongoDB ORM for Python. This is yet another simpler Python driver for MongoDB and can be used to easily interact with application data. The package is currently actively looking for contributors.
You can always drop them an email if you are a smarty pant with loads of Python MongoDB ideas!
Installation
- Install the package from PyPi with PIP using the below command:
| 1 |
$ python -m pip install -U mongoengine
| | —- | —- |
For more information on the MongoEngine MongoDB ORM for Python, visit the official docs of this package here.
Read More: Python and MongoDB Tutorial – A Guide to using Python with MongoDB
Conclusion
The popularity of MongoDB has grown over time, especially as a robust database management system. There’s no denying it – MongoDB is sometimes a challenge for beginners. There are a number of drawbacks to the components, in addition to the complexity of the documentation.
Python, being an old yet powerful programming language can produce more powerful applications when bundled with a powerful NoSQL database service like MongoDB. To make lives easier ODMs were introduced. Most MongoDB ORM for Python solve the same problem, yet they differ for a variety of reasons. They also have different levels of scope and complexity.
In this guide, I have covered the top MongoDB ORM for Python for you so you can choose from the best one suitable for your application.
参考
用于 Python 的轻量级 Mongodb ODM/ORM
https://stackoverflow.com/questions/10252100/lightweight-mongodb-odm-orm-for-python
你使用什么 Python ODM?
https://www.mongodb.com/community/forums/t/what-python-odm-do-you-use/983