200

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?

6 Answers 6

230

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.

3
  • 8
    You can have hybrid ORM/ODM frameworks, like mORMot for Delphi, Doctrine for PHP or Hibernate OGM for Java. And some SQL databases have strong support to documents, e.g. the great PostgresSQL which features JSON or JSONB data types so you can mix RDBMS and Document-oriented storage within the same table, including indexes and advanced query! Commented Jul 4, 2014 at 11:21
  • mongoose is an ODM or ORM?
    – Y.L.
    Commented Aug 8, 2018 at 3:41
  • 2
    mongoose, mongoid are all ODM's. I guess for a noSQL we can only have ODMs.
    – Surya
    Commented Nov 14, 2018 at 4:04
38

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.

1
  • 1
    Great explanation! I am still not clear if ODM/ORM are abstraction layers provided by the underlying database or by the driver libraries, or are they a separate layer between the driver and the DB? Can there be an ORM driver for a Document Database and vice versa?
    – pooya13
    Commented May 9, 2019 at 21:44
14

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.

12

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.

2
  • 1
    technically speaking, ODBC annd JDBC are API Specifications a driver implements.
    – asgs
    Commented Apr 2, 2016 at 19:02
  • 2
    Postgres too supports JSONB
    – Surya
    Commented Nov 14, 2018 at 4:05
4

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/

2

Mongoose is an example for ODM which maps between object models and MongoDB. Mongoose provides a lot of functions for querying and editing documents in a collection on the model class, as well as a rich query class to make manual queries.

Sequelize is a modern example for ORM which maps between object models and an SQL DB. Sequelize works with many Relational Databases such as Postgres, MariaDB, SQLite, MySQL, Oracle and SQL Server. Sequelize has a very similar interface to Mongoose, where you start by defining tables (instead of collections) as models in TypeScript.

Not the answer you're looking for? Browse other questions tagged or ask your own question.