SQL to MongoDB Mapping Chart
In addition to the charts that follow, you might want to consider theFrequently Asked Questions section for a selection of common questions about MongoDB.
Terminology and Concepts
The following table presents the various SQL terminology and conceptsand 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 | $lookup , embedded documents |
primary keySpecify any unique column or column combination as primary key. | primary keyIn MongoDB, the primary key is automatically set to the_id field. |
aggregation (e.g. group by) | aggregation pipelineSee the SQL to Aggregation Mapping Chart. |
SELECT INTO NEW_TABLE | $out See the SQL to Aggregation Mapping Chart. |
MERGE INTO TABLE | $merge (Available starting in MongoDB 4.2)See the SQL to Aggregation Mapping Chart. |
transactions | transactionsTipFor many scenarios, the denormalized data model(embedded documents and arrays)will continue to be optimal for your data and use casesinstead of multi-document transactions. That is, for manyscenarios, modeling your data appropriately will minimize theneed for multi-document transactions. |
Executables
The following table presents some database executables and thecorresponding MongoDB executables. This table is not meant to beexhaustive.
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 thecorresponding MongoDB statements. The examples in the table assume thefollowing conditions:
The SQL examples assume a table named
people
.The MongoDB examples assume a collection named
people
that containdocuments 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 totable-level actions and the corresponding MongoDB statements.
SQL Schema Statements | MongoDB Schema Statements |
---|---|
| Implicitly created on first insertOne() orinsertMany() operation. The primary key_id is automatically added if _id field is not specified.However, you can also explicitly create a collection:
|
| Collections do not describe or enforce the structure of itsdocuments; i.e. there is no structural alteration at thecollection level.However, at the document level, updateMany() operations can add fields to existing documents using the$set operator.
|
| Collections do not describe or enforce the structure of itsdocuments; i.e. there is no structural alteration at the collectionlevel.However, at the document level, updateMany() operations can remove fields from documents using the$unset operator.
|
|
|
|
|
|
|
For more information on the methods and operators used, see:
- db.collection.insertOne() - db.collection.insertMany() - db.createCollection() | - db.collection.updateMany() - db.collection.createIndex() - db.collection.drop() | - $set - $unset |
See also
Insert
The following table presents the various SQL statements related toinserting records into tables and the corresponding MongoDB statements.
SQL INSERT Statements | MongoDB insertOne() Statements |
---|---|
|
|
For more information, see db.collection.insertOne()
.
See also
Select
The following table presents the various SQL statements related toreading records from tables and the corresponding MongoDB statements.
Note
The find()
method always includes the _id
field in the returned documents unless specifically excluded throughprojection. Some of the SQL queries below may include an_id
field to reflect this, even if the field is not included in thecorresponding find()
query.
SQL SELECT Statements | MongoDB find() Statements |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| -or-
|
| -or-
|
|
|
|
|
| _or
|
| _or
|
| _or
|
| or, for distinct value sets that do not exceed the BSON size limit
|
| _or
|
|
|
|
|
For more information on the methods and operators used, see
- db.collection.find() - db.collection.distinct() - db.collection.findOne() - limit() - skip() - explain() - sort() - count() | - $ne - $and - $or - $gt - $lt - $exists - $lte - $regex |
See also
Update Records
The following table presents the various SQL statements related toupdating existing records in tables and the corresponding MongoDBstatements.
SQL Update Statements | MongoDB updateMany() Statements |
---|---|
|
|
|
|
For more information on the method and operators used in the examples, see:
- db.collection.updateMany() - $gt - $set - $inc |
See also
Delete Records
The following table presents the various SQL statements related todeleting records from tables and the corresponding MongoDB statements.
SQL Delete Statements | MongoDB deleteMany() Statements |
---|---|
|
|
|
|
For more information, see db.collection.deleteMany()
.
See also