SQL到MongoDB的映射图表
在本页面
除了下面的图表之外,您可能需要考虑有关MongoDB的常见问题的常见问题部分。
术语和概念
下表介绍了各种SQL术语和概念以及相应的MongoDB术语和概念。
SQL术语/概念 | MongoDB术语/概念 |
---|---|
database | |
table | |
row | |
column | |
index | |
table joins | |
primary key (指定任何唯一的列或列组合作为主键。) | |
aggregation (e.g. group by) | |
SELECT INTO NEW_TABLE | |
MERGE INTO TABLE | |
Transactions |
可执行文件
下表展示了一些数据库可执行文件和相应的MongoDB可执行文件。这个表格并不是详尽无遗的。
MongoDB | MySQL | Oracle | Informix | DB2 | |
---|---|---|---|---|---|
Database Server | mysqld | oracle | IDS | DB2 Server | |
Database Client | mysql | sqlplus | DB-Access | DB2 Client |
例子
下表展示了各种SQL语句和相应的MongoDB语句。表中的例子假设以下条件:
SQL示例假设有一个名为people的表。
MongoDB示例假设一个名为people的集合,它包含以下原型的文档:
创建和修改
下表展示了与表级操作相关的各种SQL语句以及相应的MongoDB语句。
SQL Schema语句 | MongoDB Schema语句 |
---|---|
CREATE TABLE people ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) ) | |
ALTER TABLE people ADD join_date DATETIME | |
ALTER TABLE people DROP COLUMN 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() |
有关使用的方法和运算符的更多信息,请参见:
另看:
插入
下表显示了与将记录插入表和相应的MongoDB语句有关的各种SQL语句。
SQL INSERT语句 | 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()
。
也可以看看:
选择
下表展示了与从表中读取记录相关的各种SQL语句以及相应的MongoDB语句。
注意
除非通过投影明确排除,否则[
find()
方法始终在返回的文档中包含**_id字段。 下面的某些SQL查询可能包含一个_id**字段来反映这一点,即使该字段未包含在相应的find()
查询中也是如此。
SQL SELECT 语句 | MongoDB find() 语句 |
---|---|
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 people *WHERE status = "A" | db.people.find( { status: "A" } ) |
SELECT user_id, status FROM people WHERE status = "A" | db.people.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 } ) |
SELECT FROM people *WHERE status != "A" | db.people.find( { status: { $ne: "A" } } ) |
SELECT FROM people WHERE status = "A" *AND age = 50 | db.people.find( { status: "A", age: 50 } ) |
SELECT FROM people WHERE status = "A" *OR age = 50 | db.people.find( { $or: [ { status: "A" } , { age: 50 } ] } ) |
SELECT FROM people *WHERE age > 25 | db.people.find( { age: { $gt: 25 } } ) |
SELECT FROM people *WHERE age < 25 | db.people.find( { age: { $lt: 25 } } ) |
SELECT FROM people WHERE age > 25 *AND age <= 50 | db.people.find( { age: { $gt: 25, $lte: 50 } } ) |
SELECT FROM people WHERE user_id *like "%bc%" | db.people.find( { user_id: /bc/ } )_ _or db.people.find( { user_id: { $regex: /bc/ } } ) |
SELECT FROM people WHERE user_id *like "bc%" | db.people.find( { user_id: /^bc/ } )_ _or db.people.find( { user_id: { $regex: /^bc/ } } ) |
SELECT FROM people WHERE status = "A" ORDER BY user_id *ASC | db.people.find( { status: "A" } ).sort( { user_id: 1 } ) |
SELECT FROM people WHERE status = "A" ORDER BY user_id *DESC | 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 people *WHERE age > 30 | db.people.count( { age: { $gt: 30 } } ) or db.people.find( { age: { $gt: 30 } } ).count() |
SELECT DISTINCT(status) FROM people | |
SELECT FROM people *LIMIT 1 | db.people.findOne() or db.people.find().limit(1) |
SELECT FROM people *LIMIT 5 SKIP 10 | db.people.find().limit(5).skip(10) |
EXPLAIN SELECT FROM people *WHERE status = "A" | db.people.find( { status: "A" } ).explain() |
有关使用的方法和运算符的更多信息,请参见:
另看:
更新记录
下表显示了与更新表中的现有记录和相应的MongoDB语句有关的各种SQL语句。
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 } } ) |
有关示例中使用的方法和运算符的更多信息,请参见:
另看:
删除记录
下表显示了与从表中删除记录和相应的MongoDB语句有关的各种SQL语句。
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().
另看:
进一步阅读
如果您正在考虑将SQL应用程序迁移到MongoDB,请下载《 MongoDB应用程序现代化指南》。
下载内容包括以下资源:
演示使用MongoDB进行数据建模的方法
白皮书涵盖了从RDBMS数据模型迁移到MongoDB的最佳实践和注意事项
参考MongoDB模式及其等效RDBMS
应用程序现代化记分卡
译者:杨帅
校对:杨帅
最后更新于