What is the difference between an ORM and an ODM?

DatabaseOrmOdm

Database Problem Overview


I am trying to figure out what the difference is between ORM and ODM, as far as I understand the concept, ORM (Object Relational Mapper) maps the relations between data, where as ODM (Object Document Mapper) deals with documents. Am I right in assuming that mySQL is an example of ORM and MongoDB is a example of ODM?

As I am sure you can see, I am not too familiar with the theory of the concept. Could someone please clarify the differences between the two?

Database Solutions


Solution 1 - Database

MySQL is an example of a relational database - you would use an ORM to translate between your objects in code and the relational representation of the data.

Examples of ORMs are nHibernate, Entity Framework, Dapper and more...

MongoDB is an example of a document database - you would use an ODM to translate between your objects in code and the document representation of the data (if needed).

Mandango is an example of an ODM for MongoDB.

Solution 2 - Database

An ORM maps between an Object Model and a Relational Database. An ODM maps between an Object Model and a Document Database. MySQL is not an ORM, it's a Relational Database, more specifically, a SQL Database. MongoDB is not an ODM, it's a Document Database.

Solution 3 - Database

Essencially, an ORM use a SQL database Driver like ODBC, JDBC or OLEDB to translate the object notation to relational notation and an ODM use a JSON or JSONB api to translate the Object notation to Document notation.

There are different kind of implementations under the hood.

PS: JSONB is a JSON text document notation stored in a binary format as used by MongoDB.

Solution 4 - Database

Mongoose is a good example for ODM(Object Data Model) for MongoDB in which you can directly perform operations with objects and that gets translated into the appropriate query and schema. It can be found Here at https://mongoosejs.com/

Solution 5 - Database

To understand the difference between an ORM and an ODM, I think that it would be helpful to first review the differences between a relational database, and a document database. I'll do so in a hand-wavy way.

A relational database is the one that you're probably used to. The one that stores data in tables, like this:

enter image description here

Common examples of relational databases are MySQL, Postgres, and SQLite. To query a relational database, you'd use SQL.

What about document databases? Well, with document databases, data is stored in JSON instead of in tables.

enter image description here

Actually, that's not 100% accurate. MongoDB explains:

> Documents store data in field-value pairs. The values can be a variety of types and structures, including strings, numbers, dates, arrays, or objects. Documents can be stored in formats like JSON, BSON, and XML.

Ok, so now, what is an ORM, what is an ODM, and how do they compare?

Well, ORM stands for... actually, I'll let this answer explain:

> Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase "an ORM".

Basically, in your application code, you are usually dealing with objects. But in your database, you have tables. An ORM is a library that maps between the two. As Wikipedia explains:

> This creates, in effect, a "virtual object database" that can be used from within the programming language.

Here is an example. Active Record is a popular ORM for Ruby on Rails. With Active Record, you could do something like User.find_by(name: 'David') and get back something like { id: 1, name: 'David' }. So the ORM is doing the following mapping for you:

enter image description here

And then with an ODM, it's basically doing the same thing, except for document databases. It's mapping from the objects in your application code to the documents in the database. Mongoose is a good example of an ODM. It works with MongoDB.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOdyss3usView Question on Stackoverflow
Solution 1 - DatabaseOdedView Answer on Stackoverflow
Solution 2 - DatabaseJörg W MittagView Answer on Stackoverflow
Solution 3 - DatabaseFernando SantucciView Answer on Stackoverflow
Solution 4 - DatabaseArjun AgarwalView Answer on Stackoverflow
Solution 5 - DatabaseAdam ZernerView Answer on Stackoverflow