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:
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.
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:
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.