MongoDB was developed in the Web 2.0 era. By then, most developers had been using SQL or object-relational mapping (ORM) tools from their language of choice to access RDBMS data. As such, these developers needed an easy way to get acquainted with MongoDB from their relational background.
Thankfully, there have been several attempts at making SQL to MongoDB cheat sheets that explain the MongoDB terminology in SQL terms.
On a higher level, we have the following:
- Databases and indexes (SQL databases)
- Collections (SQL tables)
- Documents (SQL rows)
- Fields (SQL columns)
- Embedded and linked documents (SQL joins)
Some more examples of common operations are shown in the following table:
SQL |
MongoDB |
Database |
Database |
Table |
Collection |
Index |
Index |
Row |
Document |
Column |
Field |
Joins |
Embed in document or link via DBRef |
CREATE TABLE employee (name VARCHAR(100)) |
db.createCollection("employee") |
INSERT INTO employees VALUES (Alex, 36) |
db.employees.insert({name: "Alex", age: 36}) |
SELECT * FROM employees |
db.employees.find() |
SELECT * FROM employees LIMIT 1 |
db.employees.findOne() |
SELECT DISTINCT name FROM employees |
db.employees.distinct("name") |
UPDATE employees SET age = 37 WHERE name = 'Alex' |
db.employees.update({name: "Alex"}, {$set: {age: 37}}, {multi: true}) |
DELETE FROM employees WHERE name = 'Alex' |
db.employees.remove({name: "Alex"}) |
CREATE INDEX ON employees (name ASC) |
db.employees.ensureIndex({name: 1}) |
Further examples of common operations can be seen at http://s3.amazonaws.com/info-mongodb-com/sql_to_mongo.pdf.