|
1 | 1 | =========== |
2 | 2 | MongoEngine |
3 | 3 | =========== |
4 | | -:Info: MongoEngine is an ORM-like layer on top of PyMongo. |
| 4 | + |
| 5 | +:Info: MongoEngine is an Object-Document Mapper (ODM) for MongoDB. |
5 | 6 | :Repository: https://github.com/MongoEngine/mongoengine |
6 | 7 | :Author: Harry Marr (http://github.com/hmarr) |
7 | 8 | :Maintainer: Bastien Gerard (http://github.com/bagerard) |
8 | 9 |
|
9 | 10 | .. image:: https://github.com/MongoEngine/mongoengine/actions/workflows/github-actions.yml/badge.svg?branch=master |
10 | | - :target: https://github.com/MongoEngine/mongoengine/actions |
| 11 | + :target: https://github.com/MongoEngine/mongoengine/actions |
11 | 12 |
|
12 | 13 | .. image:: https://coveralls.io/repos/github/MongoEngine/mongoengine/badge.svg?branch=master |
13 | | - :target: https://coveralls.io/github/MongoEngine/mongoengine?branch=master |
| 14 | + :target: https://coveralls.io/github/MongoEngine/mongoengine?branch=master |
14 | 15 |
|
15 | 16 | .. image:: https://img.shields.io/badge/code%20style-black-000000.svg |
16 | | - :target: https://github.com/ambv/black |
| 17 | + :target: https://github.com/psf/black |
17 | 18 |
|
18 | 19 | .. image:: https://pepy.tech/badge/mongoengine/month |
19 | | - :target: https://pepy.tech/project/mongoengine |
| 20 | + :target: https://pepy.tech/project/mongoengine |
20 | 21 |
|
21 | 22 | .. image:: https://img.shields.io/pypi/v/mongoengine.svg |
22 | | - :target: https://pypi.python.org/pypi/mongoengine |
23 | | - |
| 23 | + :target: https://pypi.python.org/pypi/mongoengine |
24 | 24 |
|
25 | 25 | .. image:: https://readthedocs.org/projects/mongoengine-odm/badge/?version=latest |
26 | | - :target: https://readthedocs.org/projects/mongoengine-odm/builds/ |
| 26 | + :target: https://mongoengine-odm.readthedocs.io/ |
| 27 | + |
27 | 28 |
|
28 | 29 | About |
29 | 30 | ===== |
30 | | -MongoEngine is a Python Object-Document Mapper for working with MongoDB. |
31 | | -Documentation is available at https://mongoengine-odm.readthedocs.io - there |
32 | | -is currently a `tutorial <https://mongoengine-odm.readthedocs.io/tutorial.html>`_, |
33 | | -a `user guide <https://mongoengine-odm.readthedocs.io/guide/index.html>`_, and |
34 | | -an `API reference <https://mongoengine-odm.readthedocs.io/apireference.html>`_. |
| 31 | + |
| 32 | +MongoEngine is a Python Object-Document Mapper (ODM) that provides a high-level, |
| 33 | +Pythonic API for working with MongoDB. It builds on top of PyMongo and offers |
| 34 | +schema enforcement, validation, inheritance, and both synchronous and |
| 35 | +asynchronous query APIs. |
| 36 | + |
| 37 | +Documentation is available at: |
| 38 | +https://mongoengine-odm.readthedocs.io |
| 39 | + |
| 40 | +Including: |
| 41 | + |
| 42 | +- Tutorial |
| 43 | +- User Guide |
| 44 | +- API Reference |
| 45 | + |
35 | 46 |
|
36 | 47 | Supported MongoDB Versions |
37 | 48 | ========================== |
38 | | -MongoEngine is currently tested against MongoDB v4.4, v5.0, v6.0, v7.0 and |
39 | | -v8.0. Future versions should be supported as well, but aren't actively tested |
40 | | -at the moment. Make sure to open an issue or submit a pull request if you |
41 | | -experience any problems with a more recent MongoDB versions. |
| 49 | + |
| 50 | +MongoEngine is tested against the following MongoDB versions: |
| 51 | + |
| 52 | +- MongoDB 4.2 |
| 53 | +- MongoDB 4.4 |
| 54 | +- MongoDB 5.0 |
| 55 | +- MongoDB 6.0 |
| 56 | +- MongoDB 7.0 |
| 57 | +- MongoDB 8.0 |
| 58 | + |
| 59 | +Newer MongoDB versions are expected to work. Please report issues if encountered. |
| 60 | + |
42 | 61 |
|
43 | 62 | Installation |
44 | 63 | ============ |
45 | | -We recommend the use of `virtualenv <https://virtualenv.pypa.io/>`_ and of |
46 | | -`pip <https://pip.pypa.io/>`_. You can then use ``python -m pip install -U mongoengine``. |
47 | | -You may also have `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ |
48 | | -and thus you can use ``easy_install -U mongoengine``. Another option is |
49 | | -`pipenv <https://docs.pipenv.org/>`_. You can then use ``pipenv install mongoengine`` |
50 | | -to both create the virtual environment and install the package. Otherwise, you can |
51 | | -download the source from `GitHub <https://github.com/MongoEngine/mongoengine>`_ and |
52 | | -run ``python setup.py install``. |
53 | 64 |
|
54 | | -The support for Python2 was dropped with MongoEngine 0.20.0 |
| 65 | +We recommend using ``virtualenv`` and ``pip``: |
| 66 | + |
| 67 | +.. code-block:: shell |
| 68 | +
|
| 69 | + python -m pip install -U mongoengine |
| 70 | +
|
| 71 | +Alternatively: |
| 72 | + |
| 73 | +.. code-block:: shell |
| 74 | +
|
| 75 | + pip install mongoengine |
| 76 | +
|
| 77 | +Python 3.8+ is required. Python 2 support was dropped in MongoEngine 0.20.0. |
| 78 | + |
55 | 79 |
|
56 | 80 | Dependencies |
57 | 81 | ============ |
58 | | -All of the dependencies can easily be installed via `python -m pip <https://pip.pypa.io/>`_. |
59 | | -At the very least, you'll need these two packages to use MongoEngine: |
60 | 82 |
|
61 | | -- pymongo>=3.12 |
| 83 | +Core dependency: |
62 | 84 |
|
63 | | -If you utilize a ``DateTimeField``, you might also use a more flexible date parser: |
| 85 | +- pymongo >= 3.12 |
64 | 86 |
|
65 | | -- dateutil>=2.1.0 |
| 87 | +Optional dependencies: |
66 | 88 |
|
67 | | -If you need to use an ``ImageField`` or ``ImageGridFsProxy``: |
| 89 | +- python-dateutil (for DateTimeField parsing) |
| 90 | +- Pillow (for ImageField / GridFS) |
| 91 | +- blinker (for signals) |
68 | 92 |
|
69 | | -- Pillow>=7.0.0 |
70 | 93 |
|
71 | | -If you need to use signals: |
| 94 | +Synchronous Usage |
| 95 | +================= |
72 | 96 |
|
73 | | -- blinker>=1.3 |
| 97 | +A simple synchronous example: |
74 | 98 |
|
75 | | -Examples |
76 | | -======== |
77 | | -Some simple examples of what MongoEngine code looks like: |
| 99 | +.. code-block:: python |
78 | 100 |
|
79 | | -.. code :: python |
80 | 101 | import datetime |
81 | | - from mongoengine import * |
| 102 | + from mongoengine import ( |
| 103 | + connect, |
| 104 | + Document, |
| 105 | + StringField, |
| 106 | + DateTimeField, |
| 107 | + ListField, |
| 108 | + ) |
82 | 109 |
|
83 | | - connect('mydb') |
| 110 | + connect("mydb") |
84 | 111 |
|
85 | 112 | class BlogPost(Document): |
86 | 113 | title = StringField(required=True, max_length=200) |
87 | | - posted = DateTimeField(default=lambda: datetime.datetime.now(datetime.timezone.utc)) |
| 114 | + posted = DateTimeField(default=datetime.datetime.utcnow) |
88 | 115 | tags = ListField(StringField(max_length=50)) |
89 | | - meta = {'allow_inheritance': True} |
90 | | -
|
91 | | - class TextPost(BlogPost): |
92 | | - content = StringField(required=True) |
93 | | -
|
94 | | - class LinkPost(BlogPost): |
95 | | - url = StringField(required=True) |
96 | | -
|
97 | | - # Create a text-based post |
98 | | - >>> post1 = TextPost(title='Using MongoEngine', content='See the tutorial') |
99 | | - >>> post1.tags = ['mongodb', 'mongoengine'] |
100 | | - >>> post1.save() |
101 | | -
|
102 | | - # Create a link-based post |
103 | | - >>> post2 = LinkPost(title='MongoEngine Docs', url='hmarr.com/mongoengine') |
104 | | - >>> post2.tags = ['mongoengine', 'documentation'] |
105 | | - >>> post2.save() |
106 | | -
|
107 | | - # Iterate over all posts using the BlogPost superclass |
108 | | - >>> for post in BlogPost.objects: |
109 | | - ... print('===', post.title, '===') |
110 | | - ... if isinstance(post, TextPost): |
111 | | - ... print(post.content) |
112 | | - ... elif isinstance(post, LinkPost): |
113 | | - ... print('Link:', post.url) |
114 | | - ... |
115 | | -
|
116 | | - # Count all blog posts and its subtypes |
117 | | - >>> BlogPost.objects.count() |
118 | | - 2 |
119 | | - >>> TextPost.objects.count() |
120 | | - 1 |
121 | | - >>> LinkPost.objects.count() |
122 | | - 1 |
123 | | -
|
124 | | - # Count tagged posts |
125 | | - >>> BlogPost.objects(tags='mongoengine').count() |
126 | | - 2 |
127 | | - >>> BlogPost.objects(tags='mongodb').count() |
128 | | - 1 |
| 116 | +
|
| 117 | + post = BlogPost( |
| 118 | + title="Using MongoEngine", |
| 119 | + tags=["mongodb", "mongoengine"], |
| 120 | + ) |
| 121 | + post.save() |
| 122 | +
|
| 123 | + count = BlogPost.objects(tags="mongoengine").count() |
| 124 | + print(count) |
| 125 | +
|
| 126 | +
|
| 127 | +Async Usage |
| 128 | +=========== |
| 129 | + |
| 130 | +MongoEngine provides a **fully supported asyncio-native API**. |
| 131 | +The async API mirrors the synchronous API and uses ``.aobjects`` along with |
| 132 | +``await`` for all I/O operations. |
| 133 | + |
| 134 | +Async support is **first-class** and designed for modern Python applications. |
| 135 | + |
| 136 | +.. code-block:: python |
| 137 | +
|
| 138 | + import asyncio |
| 139 | + from mongoengine import ( |
| 140 | + Document, |
| 141 | + StringField, |
| 142 | + async_connect, |
| 143 | + ) |
| 144 | +
|
| 145 | + async_connect("mydb") |
| 146 | +
|
| 147 | + class User(Document): |
| 148 | + name = StringField(required=True) |
| 149 | +
|
| 150 | + async def main(): |
| 151 | + # Create |
| 152 | + alice = await User.aobjects.create(name="Alice") |
| 153 | +
|
| 154 | + # Query |
| 155 | + first = await User.aobjects.first() |
| 156 | + assert first == alice |
| 157 | +
|
| 158 | + # Update |
| 159 | + await User.aobjects(name="Alice").update(set__name="Alicia") |
| 160 | +
|
| 161 | + # Delete |
| 162 | + await User.aobjects(name="Alicia").delete() |
| 163 | +
|
| 164 | + asyncio.run(main()) |
| 165 | +
|
| 166 | +
|
129 | 167 |
|
130 | 168 | Tests |
131 | 169 | ===== |
132 | | -To run the test suite, ensure you are running a local instance of MongoDB on |
133 | | -the standard port and have ``pytest`` installed. Then, run ``pytest tests/``. |
134 | 170 |
|
135 | | -To run the test suite on every supported Python and PyMongo version, you can |
136 | | -use ``tox``. You'll need to make sure you have each supported Python version |
137 | | -installed in your environment and then: |
| 171 | +To run the test suite locally: |
| 172 | + |
| 173 | +.. code-block:: shell |
| 174 | +
|
| 175 | + pytest tests/ |
| 176 | +
|
| 177 | +To run against all supported Python and MongoDB versions: |
138 | 178 |
|
139 | 179 | .. code-block:: shell |
140 | 180 |
|
141 | | - # Install tox |
142 | | - $ python -m pip install tox |
143 | | - # Run the test suites |
144 | | - $ tox |
| 181 | + python -m pip install tox |
| 182 | + tox |
| 183 | +
|
145 | 184 |
|
146 | 185 | Community |
147 | 186 | ========= |
148 | | -- `MongoEngine Users mailing list |
149 | | - <http://groups.google.com/group/mongoengine-users>`_ |
150 | | -- `MongoEngine Developers mailing list |
151 | | - <http://groups.google.com/group/mongoengine-dev>`_ |
| 187 | + |
| 188 | +- MongoEngine Users mailing list: |
| 189 | + http://groups.google.com/group/mongoengine-users |
| 190 | +- MongoEngine Developers mailing list: |
| 191 | + http://groups.google.com/group/mongoengine-dev |
| 192 | + |
152 | 193 |
|
153 | 194 | Contributing |
154 | 195 | ============ |
155 | | -We welcome contributions! See the `Contribution guidelines <https://github.com/MongoEngine/mongoengine/blob/master/CONTRIBUTING.rst>`_ |
| 196 | + |
| 197 | +Contributions are welcome! |
| 198 | + |
| 199 | +Please see: |
| 200 | +https://github.com/MongoEngine/mongoengine/blob/master/CONTRIBUTING.rst |
0 commit comments