Welcome to Ming Documentation¶
Ming is an Object Document Mapper (like an ORM but for Document based databases), for MongoDB. Ming extends pymongo providing:
Declarative Models
Schema Validation and Conversion
Schema Evolution
Pure InMemory MongoDB Implementation
Unit of Work
Identity Map
One-To-Many, Many-To-One and Many-To-Many Relations
Getting Started¶
To get started with Ming just install it with:
$ pip install ming
Connecting to MongoDB¶
Before we start, make sure you have a copy of MongoDB running.
First thing needed to start using Ming is to tell it how to connect to our instance of mongod.
For this we use the create_datastore()
function, this function creates a connection
to the MongoDB instance, replicaset or cluster specified by the given URL:
from ming import create_datastore
from ming.odm import ThreadLocalODMSession
session = ThreadLocalODMSession(
bind=create_datastore('mongodb://myuser:mypassword@localhost:27017/odm_welcome')
)
The ThreadLocalODMSession
is the object all your models will use to interact with
MongoDB and can be directly used to perform low-level mongodb oprations.
While this provides no particular benefit over using pymongo directly
it already permits to create and query documents:
>>> session.db.wiki_page.insert_one({'title': 'FirstPage',
... 'text': 'This is my first page'})
InsertOneResult([ObjectId('66e1e8c2a8572d7f63002562')], acknowledged=True)
>>> session.db.wiki_page.find_one({'title': 'FirstPage'})
{'_id': ObjectId('66e1e8c2a8572d7f63002562'), 'title': 'FirstPage', 'text': 'This is my first page'}
Using Models¶
Now that we know how to connect to the Database we can declare models which will be persisted on the database their session is associated to:
from ming import schema
from ming.odm import FieldProperty
from ming.odm.declarative import MappedClass
class WikiPage(MappedClass):
class __mongometa__:
session = session
name = 'wiki_page'
_id = FieldProperty(schema.ObjectId)
title = FieldProperty(schema.String(required=True))
text = FieldProperty(schema.String(if_missing=''))
Models can be created by creating them and flushing their changes to the database.
A Model can then be queried back with the Model.query.find()
method:
>>> # Creating a Document is enough to register it into the UnitOfWork
>>> WikiPage(title='FirstPage',
... text='This is my first page')
<WikiPage _id=ObjectId('66e1e8c2a8572d7f63002563')
title='FirstPage' text='This is my first page'>
>>> # Flush the unit of work to save changes on DB
>>> session.flush()
>>>
>>> wp = WikiPage.query.find({'title': 'FirstPage'}).first()
>>> wp
<WikiPage _id=ObjectId('66e1e8c2a8572d7f63002562')
title='FirstPage' text='This is my first page'>
To start working with Ming continue with the Ming ODM User Guide
Community¶
To get help with using Ming, use the Ming Users mailing list or the TurboGears Users mailing list.
Contributing¶
Yes please! We are always looking for contributions, additions and improvements.
The source is available on GitHub and contributions are always encouraged. Contributions can be as simple as minor tweaks to this documentation or to ming itself.
To contribute, fork the project and send a pull request.
Changes¶
See the Ming News / Release Notes for a full list of changes to Ming
Documentation Content¶
- Ming ODM User Guide
- Ensuring Indexing
- Model Evolution and Migrations
- ODM Event Interfaces
- Polymorphic Entities
- Custom Schemas and Properties
- Ming Foundation Layer
- API Reference
- Ming News / Release Notes
- 0.14.0 (Aug 1, 2024)
- 0.13.0 (Mar 16, 2023)
- 0.12.2 (Nov 15, 2022)
- 0.12.1 (Sep 13, 2022)
- 0.12.0 (Jun 2, 2022)
- 0.11.2 (Oct 15, 2021)
- 0.11.1 (Sep 9, 2021)
- 0.11.0 (Sep 9, 2021)
- 0.10.2 (Jun 19, 2020)
- 0.10.1 (Jun 17, 2020)
- 0.10.0 (Jun 8, 2020)
- 0.9.2 (Mar 12, 2020)
- 0.9.1 (May 15, 2019)
- 0.9.0 (Feb 22, 2019)
- 0.8.1 (Feb 22, 2019)
- 0.8.0 (Jan 15, 2019)
- 0.7.1 (Nov 30, 2018)
- 0.7.0 (May 10, 2018)
- 0.5.7 (Mar 12, 2020)
- 0.5.6 (Apr 2, 2018)
- 0.6.1 (Sep 27, 2017)
- 0.6.0 (Sep 24, 2017)
- 0.5.5 (Nov 30, 2016)
- 0.5.4 (Apr 29, 2016)
- 0.5.3 (Oct 18, 2015)
- 0.5.2 (Apr 16, 2015)
- 0.5.1 (Apr 6, 2015)
- 0.5.0 (Jun 5, 2014)
- 0.4.7 (Apr 16, 2014)
- 0.4.6 (Apr 4, 2014)
- 0.4.5 (Apr 4, 2014)
- 0.4.4 (Mar 10, 2014)
- 0.4.3 (Jan 7, 2014)
- 0.4.2 (Sep 26, 2013)
- 0.4.1 and 0.3.9 (Aug 30, 2013)
- 0.4 (June 28, 2013)
- 0.3.2 through 0.3.8
- 0.3.2 (rc1) (January 8, 2013)
- 0.3.2 (dev) (July 26, 2012)
- 0.3 (March 6, 2012)
- 0.2.1