Object Data Models to Interact with Data in MongoDB

Object Data Models to Interact with Data in MongoDB
Photo by Ryunosuke Kikuno / Unsplash

In our previous post on Data Modeling for Mongo, we discussed the process of creating classes to provide a schema to an otherwise schemaless database so that users are able to know about the structure of data in the database and provide helper functions to classes which would allow for richer interactions with the data. In this next article, we discuss the helpfulness of using Object Data Models (ODMs) to interact with data in a schemaless database.

Similarities between Object Relational Models and Object Data Models

An object-document mapper (ODM) is a tool or library that allows you to work with data stored in a document-oriented database using object-oriented concepts and techniques. An ODM provides a way to map the data stored in the database to objects in an object-oriented programming language, and it provides methods for storing and retrieving data using those objects.

An object-relational mapper (ORM) is a similar tool or library that allows you to work with data stored in a relational database using object-oriented concepts and techniques. An ORM provides a way to map the data stored in the database to objects in an object-oriented programming language, and it provides methods for storing and retrieving data using those objects.

The main difference between an ODM and an ORM is the type of database that they are used with. An ODM is used with a document-oriented database such as MongoDB, which stores data in the form of JSON-like documents. An ORM is used with a relational database such as MySQL or PostgreSQL, which stores data in the form of tables with rows and columns.

Overall, an ODM and an ORM are similar in that they both provide a way to work with data stored in a database using object-oriented concepts and techniques, but they are used with different types of databases. If you have used an ORM before, then you'll be well on your way to understanding ODMs as they are functionaly similar, where as an ODM is used with a document-oriented databases.

The Helpfulness of an ODM for Querying Data in MongoDB

A ODM is useful for querying data because it provides a way to work with data stored in a document-oriented database using object-oriented concepts and techniques. This can make it easier and more intuitive to query data stored in a document-oriented database such as MongoDB, especially if you are familiar with object-oriented programming.

With an ODM, you can use objects and object-oriented concepts to build queries for finding data in the database. For example, you can use methods such as find() to retrieve data from the database, and you can use filters and other criteria to specify the data that you want to retrieve.

In addition to making it easier to build queries, an ODM can also make it easier to work with the data that you retrieve from the database. When you retrieve data using an ODM, the data is automatically mapped to objects in your programming language, which means that you can work with the data using object-oriented techniques such as object properties and methods. This can make it easier to manipulate and process the data, and it can make your code more readable and maintainable.

A Python Example with MongoEngine

MongoEngine is one of many different ODMs available for Python to interact with data in a mongo mongo database. You are, of course, free to use any other language and associated ODM libraries for your language to interact with data in MongoDB. The general concepts will be applicable to libraries in other languages.

Take for example the definition of an ODM below using mongoengine:

from mongoengine import Document, StringField, IntField

# Define the ORM for the "Person" entity
class Person(Document):
    name = StringField(required=True)
    age = IntField(required=True)
    gender = StringField(required=True)
    address = StringField(required=True)
    phone = StringField(required=True)

The example above provides information about the available fields of an object using class variables defined as fields. The benefits of this Data Model, in addition to providing the required properties of this class, also provides tooling to query for data inside a database through these fields.

With MongoDB being the example database the example below demonstrates connecting to the database, creating a person object and saving it to the database and querying back the person object:

from mongoengine import connect

# Connect to the MongoDB server
# Refer to https://docs.mongoengine.org/guide/connecting.html for other ways of connecting
connect("mongodb://localhost:27017/")

# Create a new Person object
person = Person(name="John Smith", age=35, gender="male", address="123 Main St.", phone="123-456-7890")
person.save()

# querying for the person
for p in Person.objects(name='John Smith'):
    print(p)

While in this case, the Person.objects(name='John Smith') query was used, MongoEngine supports a variety of different query methods to search for data in the database:

  • __lt (less than): Find objects where the specified field has a value less than the provided value.
  • __lte (less than or equal to): Find objects where the specified field has a value less than or equal to the provided value.
  • __gt (greater than): Find objects where the specified field has a value greater than the provided value.
  • __gte (greater than or equal to): Find objects where the specified field has a value greater than or equal to the provided value.
  • __ne (not equal to): Find objects where the specified field has a value that is not equal to the provided value.
  • __in: Find objects where the specified field has a value that is in the provided list of values.
  • __nin: Find objects where the specified field has a value that is not in the provided list of values.
  • __exists: Find objects where the specified field exists (or does not exist).

and these can be used by appending them to the field name to generate a query in the following manner Person.objects(age__gt=31)which would query for persons over the age of 31. A more detailed list of the different possible queries is provided in their documentation.

Further Reading

While the above is a specific example for MongoEngine with Python there are a variety of different libraries are available for exploration for each popular language: