SQL to MongoDB Mapping Chart
In addition to the charts that follow, you might want to consider the Frequently Asked Questions section for a selection of common questions about MongoDB.
Terminology and Concepts
The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.
SQL Terms/Concepts | MongoDB Terms/Concepts |
---|---|
database | database |
table | collection |
row | document or BSON document |
column | field |
index | index |
table joins | embedded documents and linking |
primary key Specify any unique column or column combination as primary key. |
In MongoDB, the primary key is automatically set to the_id field. |
aggregation (e.g. group by) |
aggregation pipeline See the SQL to Aggregation Mapping Chart. |
Executables
The following table presents some database executables and the corresponding MongoDB executables. This table is not meant to be exhaustive.
MongoDB | MySQL | Oracle | Informix | DB2 | |
---|---|---|---|---|---|
Database Server | mongod | mysqld | oracle | IDS | DB2 Server |
Database Client | mongo | mysql | sqlplus | DB-Access | DB2 Client |
Examples
The following table presents the various SQL statements and the corresponding MongoDB statements. The examples in the table assume the following conditions:
The SQL examples assume a table named users.
-
The MongoDB examples assume a collection named users that contain documents of the following prototype:
{
_id: ObjectId("509a8fb2f3f4948bd2f983a0"),
user_id: "abc123",
age: 55,
status: 'A'
}
Create and Alter
The following table presents the various SQL statements related to table-level actions and the corresponding MongoDB statements.
SQL Schema Statements | MongoDB Schema Statements |
---|---|
CREATE TABLE users ( |
Implicitly created on first insert() operation. The primary key _id is automatically added if _id field is not specified. db.users.insert( { However, you can also explicitly create a collection: db.createCollection("users") |
ALTER TABLE users |
Collections do not describe or enforce the structure of its documents; i.e. there is no structural alteration at the collection level. However, at the document level, update() operations can add fields to existing documents using the $set operator. db.users.update( |
ALTER TABLE users |
Collections do not describe or enforce the structure of its documents; i.e. there is no structural alteration at the collection level. However, at the document level, update() operations can remove fields from documents using the $unset operator. db.users.update( |
CREATE INDEX idx_user_id_asc |
db.users.createIndex( { user_id: 1 } ) |
CREATE INDEX |
db.users.createIndex( { user_id: 1, age: -1 } ) |
DROP TABLE users |
db.users.drop() |
For more information, see db.collection.insert(), db.createCollection(),db.collection.update(), $set, $unset, db.collection.createIndex(), indexes,db.collection.drop(), and Data Modeling Concepts.
Insert
The following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements.
SQL INSERT Statements | MongoDB insert() Statements |
---|---|
INSERT INTO users(user_id, |
db.users.insert( |
For more information, see db.collection.insert().
Select
The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.
SQL SELECT Statements | MongoDB find() Statements |
---|---|
SELECT * |
db.users.find() |
SELECT id, |
db.users.find( |
SELECT user_id, status |
db.users.find( |
SELECT * |
db.users.find( |
SELECT user_id, status |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( { user_id: /bc/ } ) |
SELECT * |
db.users.find( { user_id: /^bc/ } ) |
SELECT * |
db.users.find( { status: "A" } ).sort( { user_id: 1 } ) |
SELECT * |
db.users.find( { status: "A" } ).sort( { user_id: -1 } ) |
SELECT COUNT(*) |
db.users.count() or db.users.find().count() |
SELECT COUNT(user_id) |
db.users.count( { user_id: { $exists: true } } ) or db.users.find( { user_id: { $exists: true } } ).count() |
SELECT COUNT(*) |
db.users.count( { age: { $gt: 30 } } ) or db.users.find( { age: { $gt: 30 } } ).count() |
SELECT DISTINCT(status) |
db.users.distinct( "status" ) |
SELECT * |
db.users.findOne() or db.users.find().limit(1) |
SELECT * |
db.users.find().limit(5).skip(10) |
EXPLAIN SELECT * |
db.users.find( { status: "A" } ).explain() |
For more information, see db.collection.find(), db.collection.distinct(),db.collection.findOne(), $ne $and, $or, $gt, $lt, $exists, $lte, $regex, limit(),skip(), explain(), sort(), and count().
Update Records
The following table presents the various SQL statements related to updating existing records in tables and the corresponding MongoDB statements.
SQL Update Statements | MongoDB update() Statements |
---|---|
UPDATE users |
db.users.update( |
UPDATE users |
db.users.update( |
For more information, see db.collection.update(), $set, $inc, and $gt.
Delete Records
The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements.
SQL Delete Statements | MongoDB remove() Statements |
---|---|
DELETE FROM users |
db.users.remove( { status: "D" } ) |
DELETE FROM users |
db.users.remove({}) |
For more information, see db.collection.remove().
SQL to Aggregation Mapping Chart
The aggregation pipeline allows MongoDB to provide native aggregation capabilities that corresponds to many common data aggregation operations in SQL.
The following table provides an overview of common SQL aggregation terms, functions, and concepts and the corresponding MongoDB aggregation operators:
SQL Terms, Functions, and Concepts | MongoDB Aggregation Operators |
---|---|
WHERE | $match |
GROUP BY | $group |
HAVING | $match |
SELECT | $project |
ORDER BY | $sort |
LIMIT | $limit |
SUM() | $sum |
COUNT() | $sum |
join | No direct corresponding operator; however, the$unwind operator allows for somewhat similar functionality, but with fields embedded within the document. |
Examples
The following table presents a quick reference of SQL aggregation statements and the corresponding MongoDB statements. The examples in the table assume the following conditions:
The SQL examples assume two tables, orders and order_lineitem that join by theorder_lineitem.order_id and the orders.id columns.
-
The MongoDB examples assume one collection orders that contain documents of the following prototype:
{
cust_id: "abc123",
ord_date: ISODate("2012-11-02T17:04:11.102Z"),
status: 'A',
price: 50,
items: [ { sku: "xxx", qty: 25, price: 1 },
{ sku: "yyy", qty: 25, price: 1 } ]
}
SQL Example | MongoDB Example | Description |
---|---|---|
SELECT COUNT(*) AS count |
db.orders.aggregate( [ |
Count all records fromorders |
SELECT SUM(price) AS total |
db.orders.aggregate( [ |
Sum theprice field from orders |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id, sum theprice field. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id, sum theprice field, results sorted by sum. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id,ord_dategrouping, sum the pricefield. Excludes the time portion of the date. |
SELECT cust_id, |
db.orders.aggregate( [ |
For cust_idwith multiple records, return thecust_id and the corresponding record count. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id,ord_dategrouping, sum the pricefield and return only where the sum is greater than 250. Excludes the time portion of the date. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id with status A, sum the pricefield. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id with status A, sum the pricefield and return only where the sum is greater than 250. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id, sum the corresponding line item qtyfields associated with the orders. |
SELECT COUNT(*) |
db.orders.aggregate( [ |