MongoDB, ORM, and Ming

Just a happy childhood memory and inspiration to share a useful library on a wet June weekend in Ireland.

Ming, of course, is a Python library.

My lazy search of Conda didn’t suggest that there is support for Conda install.

So, and despite all the dire warnings I sometimes read about not using Pip with Anaconda, I went ahead and used Pip install.

Like, Ming the Merciless, I am happy to make sacrifices!Ming is a library that implements ORM for MongoDB.

Let us continue the discussion with some Python code.

FilmsTypically with any movie, we might be interested in the film title, who directed the film, and when it was first released.

We are also interested in tags, categories and stars.

The labels and categories allow us to group our film with similar films and that would enable us to recommend movies to users.

Metadata usually contains some form of popularity measure.

If we are running a movie club site then perhaps our membership might like to leave their comments for future users.

There would likely be zero or many comments about any given movie.

Implementing the film data structure using MongoDB and Ming is illustrated in our ming.

py code above.

First, we have to import ming and ming ODM.

Next, we must create a session and bind our MongoDB instance to the session.

In our case, we are using localhost:27017 standard local installations.

For our purposes in this and some associated articles, we are using a Database called ‘Postgres_compare’.

In the SQL relational world, we prefer Postgres to MySQL.

Each film can have comments, and we create a RelationProperty linking ‘comments’ to the ‘FilmComments’ collection.

CommentsIf we run a movie club, then the members will want to discuss their viewing and perspectives on the films they have watched.

Providing a discussion forum can be achieved in several different ways.

For now, since we talk about ORM, we introduce a comments class.

A single comment has a text containing the perspective, a date when the user wrote the comment, and the user who entered the comment.

We use the ForeignIdProperty and RelationProperty to link the comment(s) to the film.

At this stage, we have a MongoDB collection that represents all our films.

Each film has metadata and a key to link the movie with membership comments.

Also, we have a collection of Comments and a relational join to the respective film the subject of the comments.

from ming.

odm import MapperMapper.

compile_all()Once you have all the data descriptions completed, it is essential to run the compile_all() method and ensure that all schema validation details are ready.

With that said, we have completed our ORM model for films.

You can use Compose or Robo 3T to examine the MongoDB databaseAs described, we created a collection called Films and a collection called Film Comments.

We see those in the Robo 3T screenshot above to the right-hand side.

A screenshot of a document, in the File Comments collection, that is a comment about a specific filmA screenshot of a document, in the Films Collection, showing the film details and associated metadata.

Working with the ORM modelNow for the fun part, though some might say the definition of the model, through an Entity Relationship Diagram, Primary and Foreign Keys, and data elements is the fun part, we disagree.

The fun part is building up the processing and exploiting the model to do real things.

film = Films(title='new one', director='Me', year='2020')session.

flush()Our first step is to go ahead and create a film entry.

With just two lines of code, we can make our film entry and push that right to MongoDB.

Isn’t that amazing!film = Films.


get(title='new one')film.

metadataWe can ask for a single film by title and display the associated metadata.

A small screenshot showing the setting of a film’s metadataWe can update a film’s metadata using the .

append() method and please notice, line 188, that our tags, categories, and stars are now list objects.


_id,text='Will be crap', date=datetime.


now(),user='mlex')Next, we can add a comment to a film.

We have to pass in the ID of our movie to make the connection between the feedback and the film under discussion.

Requesting a film and printing out the comments.

At this point, you might be thinking that we need a query to join the two collections, the screenshot shows that we can ask for the film, by title, and iterate over the comments linked to the film.

That seems cool!film.



clear()We can delete any film with the .

delete() method, but you must first retrieve a film using Films.



To make the database commit, we have to invoke the .

flush() method of the session.

A small word of warning, though.

The .

delete() process only appears to delete from a single collection.

Even though we deleted a film, our code did not automatically remove the comments in the ‘FilmComments’ group.

While the film record is gone, some of the data remain, and some analytics might map the ‘FilmComments’ collection, and hence a better delete strategy would be needed.

Using the ORM model, we can see how easy it is to add a film, change the metadata, add a comment and then delete the movie.

The simplistic handling of Database CRUD operations is why using an ORM approach is a great idea, and Ming handles the whole thing for us.

Postgres SQLHaving taken a close look at how Ming helps us to implement ORM with MongoDB we can now take a look at Postgres ORM using the ‘sqlalchemy’ library.

At first glance, the models are not as dissimilar as one might have otherwise expected with the difference between SQL and NoSQL.

There is however a world of difference between tables with rows and documents with sub-documents.

Python Dictionaries, Lists, Sets and Tuples do not go into tables and rows so easily.

We use DBeaver on our relational sources and Robo3T on our NoSQL sourcesEarthlings and the final judgementWriting in the spirit of fun, with some serious work in the background, our article was indeed an adventure from Childhood to Schema based noSQL using the Ming library.

Ming is certainly a powerful library, it surprised us, and we will certainly be adding it to our fraternity toolbox.

What you will do depends entirely on you but you should definitely go ahead and check it out.


. More details

Leave a Reply