Lesson 2: Object Relational Mappers in MongoDB / Learn

Video Transcript (English)

In this video, you’ll learn how object mappers fit into MongoDB. Object mappers are also known as Object Relational Mappers (or ORMs).

Coming from a relational database background, you're probably well acquainted with using some type of ORM. ORMs are a library used in an application that enables us to map tables to workable objects.

With MongoDB, documents are typically structured in a similar way to how they will be used by the application. By storing data this way, we can reduce, if not eliminate, the need for joins or ORMs.

ORMs often pull data from multiple tables and perform joins to map table values to instantiated objects that we need to use in an application.

In relational databases, the access layer is also a key feature of ORMs. It provides simplified access to data in persistent storage.

MongoDB has incorporated this logic into each of its drivers. A driver handles all interactions between your application (the client) and the database (server or service) in an idiomatic way for each programming language.

Of course, while ORMs may not be a necessity with MongoDB, in some cases you may want the extra application side validation, middleware functionality, or convenience methods and helpers that are provided by using one.

If you’re interested in using an ORM, you have plenty of options to choose from, such as: Mongoose and Prisma for Node.js, Doctrine for PHP, and Mongoid for Ruby.

As a quick note, these are sometimes referred to as “ODMs” (Object Document Mappers), but this is not consistent, so it’s best to search for both terms if you’re interested in them.

Now let’s go through an example of a MongoDB ORM.

Here’s a simple Node project that has Mongoose already installed. This project consists of a single main function where all our logic will take place. We’ve also connected to our Atlas cluster.

Now we’ll set up application side validation.

Here’s the schema that we’ll use. Take note of the name field. We are making “name” a required field that must be a string. We can further enforce uniqueness from the application side by enforcing the correct data types.

Now that our schema is complete, we can create our User model.

Before testing, note that we’re leaving the name field blank so that we can test the application side validation. Now let’s test it by creating a new user and querying the database.

The result is an error message, which shows that the application side validation is working.

We can fix the problem by giving this user the name Mira Chan.

Now when we run the application, our user is created, which we can confirm in the console.

This is just one example of the types of issues that an ORM can help with.

But while these provide application side validation, this additional layer of abstraction between the application and the data may have a performance cost, so that’s something to be aware of.

In this lesson, you learned about using Object Relational Mappers with MongoDB. Object Relational Mappers are not a necessity, but can be helpful with things like application side validation and middleware functionality.