Setting up Python to use Mongo

I am new to Python and MongoDB and I am struggling with how to set up my classes within my project to use MongoDB. In my learning journey I came across the mongoengine and I have the following question.

I have a project that has 1 database with currently 7 collections. The collections are:
cart
country
invoice
product
session
store
user

I am currently taking a long hard look at my collections and strongly considering eliminating the country and invoice collections. Country can be combined into both the store and user collection as individual fields. (Tax can still be figured based on where the sale is taking place… from the store country) I am unsure about the invoice collection as this can be a large collection over time. (having to keep sales records for long periods of time)

But upon looking up multiple questions I came across the mongoengine… From my research, it appears that mongoengine is a third party and if I am using pymongo it seems to not be required. Am I right?

My current classes are written like this:

from dataclasses import dataclass
from model.cart import Cart


@dataclass
class User(object):
    
    def __init__(self, first_name, last_name, phone, email, uid, cart):
        self.first_name = first_name
        self.last_name = last_name
        self.phone = phone
        self.email = email
        self.uid = uid
        self.cart = cart
       

    def __getattr__(self, key):
        return getattr(self, key)

    def __repr__(self):
        return "first_name: {}, last_name: {}, uid: {}, cart: {}, card: {}".format(self.first_name, self.last_name, self.uid, self.cart, )


    first_name: str
    last_name: str
    phone: str
    email: str
    uid: str
    cart: Cart

I find the documentation on mongoengine to define classes quite differently. They define classes like this:

class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)

I cannot find any documentation that tells me what format I need to use… Can you point me in the right direction for clarification? I am dying to read about this and whether I need to use the schema outlined in the mongoengine or if I can use the schema like all the tutorials explain in Python. I.E. can I use the standard class definition or do I need to use the mongoengine verison.

From what I can gather, the mongoengine version enforces validation. If I don’t use this format, I’m assuming I need to program in the validation. (the required portion and also max length, etc)

Am I on the right track?

Hi @David_Thompson and welcome in the MongoDB Community :muscle: !

mongoengine is a third party Object-Mapper library. Just like Mongoose is an object mapper for Node.js that isn’t required nor mandatory, mongoengine isn’t required either. Pymongo can be used on its own, just like the MongoDB Node.js driver can be used on its own.

MongoDB is a schemafree / schemaless database. I’m probably going to start a debate just by stating this already, but what I mean by this is that MongoDB doesn’t need to impose a schema on the docs by default. mongoengine and Mongoose help you impose a schema on your MongoDB collections on the back-end side.

This doesn’t prevent you from modifying the docs manually by any other mean and breaking these “contracts” in your back-end.

Another solution if you need or want to create some rules in your MongoDB documents is to use the JSON Schema validators. By adding these rules directly in the MongoDB collection, you can ensure that you cannot insert a negative price for your product (for example) whatever the mean this time (a wrong update in mongosh, etc). The validator will always keep you in check.

So to sum up, mongoengine is just here to help you map your MongoDB docs to python objects. If you feel that you don’t need it, then don’t use it. It’s an extra layer / proxy to the pymongo driver which might just add a layer of complexity in your code.

If you require / need more industrialisation, it might help.

Example of a COVID-19 data import script I have designed. It’s only using Pymongo. I scan CSV files, build the docs in memory and send them to MongoDB with insert_many(X).

If you need more help, I would recommend to check the MongoDB for Python Developer free training that we have on the MongoDB University:

Or our Python ressources / tutos on the MongoDB Developer Hub:

Especially this one:

I hope this helps.

Cheers,
Maxime.

1 Like

@MaBeuLux88 ,
Thanks!!! I will for sure check those out… While I have already taken the M220P course, there is limited exposure on how to set up the program… It mainly focuses on queries. This is the sticky point that I can see for the MongoDB University courses… There isn’t one that really takes one through the FULL development process. I wish there was, but sadly that is my achilles heal… I can get info on queries, and I can find information on schema… But practical application into a development cycle still remains an elusive thing.

1 Like