Using MongoDB with PyMongo

Sai Charan Chinta
Unibuddy Technology Blog
2 min readJun 30, 2020

PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python. Now one of the problems with using PyMongo is a lack of strong validation for the documents. Marshmallow is already extensively used in our code base and it served us really well. (For anyone curious, we are currently using pymongo==3.7.0 and marshmallow==2.18.0)

Photo by Alex wong on Unsplash

In order to solve the problem of validation, we created a base class that other models can inherit. The model class will also provide meta information like the collection name and the schema to be used. The parent class is responsible to implement the validation and also loads the response from MongoDB through the provided schema.

You need access to the pymongo client so we can create a singleton database utility class like this and then use it in other places:

The model and schema could look like this:

The BaseDocument now has access to the schema and the collection that the model belongs to. We use the collection name to get access to methods like insert_one, update_one, etc defined on the collection and the schema to validate the data being sent to mongo. It could look something like this:

Then we create a get method which retrieves the response from the DB and parses it using validate_schema method and returns the parsed data.

The create call using pymongo do not give you the complete document. We have to insert and then retrieve the document using the get defined above like this:

Please see the complete implementation of BaseDocument with all the CRUD operations here:

You can now start using the SampleModel to perform CRUD operations like this:

The sample examples are more of a guideline on how we can enforce schema and leverage pymongo. You could expand to support more methods like create_many, insert_many, find_one, etc and include error handling, custom errors, defining and creating indexes on models, etc.

You can find the complete code here

--

--