SQL to MongoDB Mapping Chart
2017-12-05 14:29
519 查看
MongoDB CRUD Operations >
SQL to MongoDB Mapping Chart
On this page
Terminology
and Concepts
Executables
Examples
Additional
Resources
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.
The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.
The following table presents some database executables and the corresponding MongoDB executables. This table is not meant to be exhaustive.
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
The MongoDB examples assume a collection named
the following prototype:
The following table presents the various SQL statements related to table-level actions and the corresponding MongoDB statements.
For more information, see:
Indexes
Data Modeling Concepts.
The following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements.
For more information, see
The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.
NOTE
The
always includes the
Some of the SQL queries below may include an
For more information, see:
Query operators:
and
The following table presents the various SQL statements related to updating existing records in tables and the corresponding MongoDB statements.
For more information, see
and
The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements.
For more information, see
Transitioning from SQL to MongoDB (Presentation)
Best Practices for Migrating from RDBMS to MongoDB (Webinar)
SQL vs. MongoDB Day 1-2
SQL vs. MongoDB Day 3-5
MongoDB vs. SQL Day 14
MongoDB and MySQL Compared
Quick Reference Cards
MongoDB Database Modernization Consulting Package
SQL to MongoDB Mapping Chart
SQL to MongoDB Mapping Chart
On this pageTerminology
and Concepts
Executables
Examples
Additional
Resources
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 | $lookup, embedded documents |
primary key Specify any unique column or column combination as primary key. | primary key In MongoDB, the primary key is automatically set to the _idfield. |
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
people.
The MongoDB examples assume a collection named
peoplethat 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 people ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) ) | Implicitly created on first insertOne()or insertMany()operation. The primary key _idis automatically added if _idfield is not specified. db.people.insertOne( { user_id: "abc123", age: 55, status: "A" } ) However, you can also explicitly create a collection: db.createCollection("people") |
ALTER TABLE people ADD join_date DATETIME | 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, updateMany()operations can add fields to existing documents using the $setoperator. db.people.updateMany( { }, { $set: { join_date: new Date() } } ) |
ALTER TABLE people DROP COLUMN join_date | 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, updateMany()operations can remove fields from documents using the $unsetoperator. db.people.updateMany( { }, { $unset: { "join_date": "" } } ) |
CREATE INDEX idx_user_id_asc ON people(user_id) | db.people.createIndex( { user_id: 1 } ) |
CREATE INDEX idx_user_id_asc_age_desc ON people(user_id, age DESC) | db.people.createIndex( { user_id: 1, age: -1 } ) |
DROP TABLE people | db.people.drop() |
db.collection.insertOne()
db.collection.insertMany()
db.createCollection()
db.collection.updateMany()
$set
$unset
db.collection.createIndex()
Indexes
db.collection.drop()
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 insertOne() Statements |
---|---|
INSERT INTO people(user_id, age, status) VALUES ("bcd001", 45, "A") | db.people.insertOne( { user_id: "bcd001", age: 45, status: "A" } ) |
db.collection.insertOne().
Select
The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.NOTE
The
find()method
always includes the
_idfield in the returned documents unless specifically excluded through projection.
Some of the SQL queries below may include an
_idfield to reflect this, even if the field is not included in the corresponding
find()query.
SQL SELECT Statements | MongoDB find() Statements |
---|---|
SELECT * FROM people | db.people.find() |
SELECT id, user_id, status FROM people | db.people.find( { }, { user_id: 1, status: 1 } ) |
SELECT user_id, status FROM people | db.people.find( { }, { user_id: 1, status: 1, _id: 0 } ) |
SELECT * FROM peopleWHERE status = "A" | db.people.find( { status: "A" } ) |
SELECT user_id, status FROM peopleWHERE status = "A" | db.people.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 } ) |
SELECT * FROM peopleWHERE status != "A" | db.people.find( { status: { $ne: "A" } } ) |
SELECT * FROM peopleWHERE status = "A" | db.people.find( { status: "A", age: 50 } ) |
SELECT * FROM peopleWHERE status = "A" | db.people.find( { $or: [ { status: "A" } , { age: 50 } ] } ) |
SELECT * FROM peopleWHERE age > 25 | db.people.find( { age: { $gt: 25 } } ) |
SELECT * FROM peopleWHERE age < 25 | db.people.find( { age: { $lt: 25 } } ) |
SELECT * FROM peopleWHERE age > 25 | db.people.find( { age: { $gt: 25, $lte: 50 } } ) |
SELECT * FROM peopleWHERE user_id like "%bc%" | db.people.find( { user_id: /bc/ } ) -or- db.people.find( { user_id: { $regex: /bc/ } } ) |
SELECT * FROM peopleWHERE user_id like "bc%" | db.people.find( { user_id: /^bc/ } ) -or- db.people.find( { user_id: { $regex: /^bc/ } } ) |
SELECT * FROM peopleWHERE status = "A" | db.people.find( { status: "A" } ).sort( { user_id: 1 } ) |
SELECT * FROM peopleWHERE status = "A" | db.people.find( { status: "A" } ).sort( { user_id: -1 } ) |
SELECT COUNT(*) FROM people | db.people.count() or db.people.find().count() |
SELECT COUNT(user_id) FROM people | db.people.count( { user_id: { $exists: true } } ) or db.people.find( { user_id: { $exists: true } } ).count() |
SELECT COUNT(*) FROM peopleWHERE age > 30 | db.people.count( { age: { $gt: 30 } } ) or db.people.find( { age: { $gt: 30 } } ).count() |
SELECT DISTINCT(status) FROM people | db.people.distinct( "status" ) |
SELECT * FROM peopleLIMIT 1 | db.people.findOne() or db.people.find().limit(1) |
SELECT * FROM peopleLIMIT 5 | db.people.find().limit(5).skip(10) |
EXPLAIN SELECT * FROM peopleWHERE status = "A" | db.people.find( { status: "A" } ).explain() |
db.collection.find()
db.collection.distinct()
db.collection.findOne()
Query operators:
$ne,
$and,
$or,
$gt,
$lt,
$exists,
$lte,
and
$regex.
limit()
skip()
explain()
sort()
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 updateMany() Statements |
---|---|
UPDATE people SET status = "C" WHERE age > 25 | db.people.updateMany( { age: { $gt: 25 } }, { $set: { status: "C" } } ) |
UPDATE people SET age = age + 3 WHERE status = "A" | db.people.updateMany( { status: "A" } , { $inc: { age: 3 } } ) |
db.collection.updateMany(),
$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 deleteMany() Statements |
---|---|
DELETE FROM people WHERE status = "D" | db.people.deleteMany( { status: "D" } ) |
DELETE FROM people | db.people.deleteMany({}) |
db.collection.deleteMany().
Additional Resources
Transitioning from SQL to MongoDB (Presentation)Best Practices for Migrating from RDBMS to MongoDB (Webinar)
SQL vs. MongoDB Day 1-2
SQL vs. MongoDB Day 3-5
MongoDB vs. SQL Day 14
MongoDB and MySQL Compared
Quick Reference Cards
MongoDB Database Modernization Consulting Package
相关文章推荐
- SQL to MongoDB Mapping Chart
- SQL to MongoDB Mapping Chart
- mongodb 语句和SQL语句对应(SQL to Aggregation Mapping Chart)
- SQL to MongoDB Mapping Chart
- mongodb:SQL to Aggregation Mapping Chart
- SQL to MongoDB: An Updated Mapping
- SQL to Mongo Mapping Chart
- SQL to Mongo Mapping Chart
- SQL to Aggregation Framework Mapping Chart
- SQL to Aggregation Mapping Chart
- SQL to Mongo Mapping Chart
- SQL to Mongo Mapping Chart
- Mapping a database user to a new SQL Server login
- Mapping a database user to a new SQL Server login
- Linq to SQL Xml Based Mapping
- mysql与MOngodb书写形式转化SQL-to-MongoDB
- SQL to MongoDB
- Mongodb is vulnerable to SQL injection in PHP at least
- Problems in mapping parameters of type Int64 to the Execute SQL Task