SQL to MongoDB Mapping ChartSQL到MongoDB的映射图
On this page本页内容
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.除了下面的图表之外,您可能还需要考虑常见问题部分,以选择有关MongoDB的常见问题。
Terminology and Concepts术语和概念
The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.下表介绍了各种SQL术语和概念以及相应的MongoDB术语和概念。
$lookup | |
primary key_id 字段。 | |
group by ) | |
SELECT INTO NEW_TABLE | $out |
MERGE INTO TABLE | $merge |
UNION ALL | $unionWith |
Tip |
Executables可执行程序
The following table presents some database executables and the corresponding MongoDB executables. This table is not meant to be exhaustive.下表显示了一些数据库可执行文件和相应的MongoDB可执行文件。本表并非详尽无遗。
MongoDB | MySQL | Oracle | Informix | DB2 | |
---|---|---|---|---|---|
mongod | mysqld | oracle | IDS | DB2 Server | |
Database Client | mongosh | 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:下表显示了各种SQL语句和相应的MongoDB语句。表中的示例假设了以下条件:
The SQL examples assume a table namedSQL示例假定一个名为people
.people
的表。The MongoDB examples assume a collection namedMongoDB示例假设一个名为people
that contain documents of the following prototype:people
的集合包含以下原型的文档:{
_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语句以及相应的MongoDB语句。
SQL Schema Statements | MongoDB Schema Statements |
---|---|
CREATE TABLE people ( | insertOne() or insertMany() operation. insertOne() 或insertMany() 操作上隐式创建。_id is automatically added if _id field is not specified. _id 字段,则会自动添加主键_id 。db.people.insertOne( { db.createCollection("people") |
ALTER TABLE people | updateMany() operations can add fields to existing documents using the $set operator. updateMany() 操作可以使用$set 运算符向现有文档添加字段。db.people.updateMany( |
ALTER TABLE people | updateMany() operations can remove fields from documents using the $unset operator. updateMany() 操作可以使用$unset 运算符从文档中删除字段。db.people.updateMany( |
CREATE INDEX idx_user_id_asc |
db.people.createIndex( { user_id: 1 } ) |
CREATE INDEX |
db.people.createIndex( { user_id: 1, age: -1 } ) |
DROP TABLE people |
db.people.drop() |
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
Insert插入
The following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements.下表显示了与将记录插入表中相关的各种SQL语句以及相应的MongoDB语句。
SQL INSERT Statements | MongoDB insertOne() Statements |
---|---|
INSERT INTO people(user_id, |
db.people.insertOne( |
For more information, see 有关详细信息,请参阅db.collection.insertOne()
.db.collection.insertOne()
。
Select选择
The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.下表显示了与从表中读取记录相关的各种SQL语句以及相应的MongoDB语句。
The find()
method always includes the _id
field in the returned documents unless specifically excluded through projection. find()
方法总是在返回的文档中包含_id
字段,除非通过投影明确排除。Some of the SQL queries below may include an 下面的一些SQL查询可能包括一个_id
field to reflect this, even if the field is not included in the corresponding find()
query._id
字段来反映这一点,即使该字段不包括在相应的find()
查询中。
SQL SELECT Statements | find() 语句 |
---|---|
SELECT * |
db.people.find() |
SELECT id, |
db.people.find( |
SELECT user_id, status |
db.people.find( |
SELECT * |
db.people.find( |
SELECT user_id, status |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( { user_id: /bc/ } )-or- db.people.find( { user_id: { $regex: /bc/ } } ) |
SELECT * |
db.people.find( { user_id: /^bc/ } )-or- db.people.find( { user_id: { $regex: /^bc/ } } ) |
SELECT * |
db.people.find( { status: "A" } ).sort( { user_id: 1 } ) |
SELECT * |
db.people.find( { status: "A" } ).sort( { user_id: -1 } ) |
SELECT COUNT(*) |
db.people.count()or db.people.find().count() |
SELECT COUNT(user_id) |
db.people.count( { user_id: { $exists: true } } )or db.people.find( { user_id: { $exists: true } } ).count() |
SELECT COUNT(*) |
db.people.count( { age: { $gt: 30 } } )or db.people.find( { age: { $gt: 30 } } ).count() |
SELECT DISTINCT(status) |
db.people.aggregate( [ { $group : { _id : "$status" } } ] ) db.people.distinct( "status" ) |
SELECT * |
db.people.findOne()or db.people.find().limit(1) |
SELECT * |
db.people.find().limit(5).skip(10) |
EXPLAIN SELECT * |
db.people.find( { status: "A" } ).explain() |
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
Update Records更新记录
The following table presents the various SQL statements related to updating existing records in tables and the corresponding MongoDB statements.下表显示了与更新表中现有记录相关的各种SQL语句以及相应的MongoDB语句。
updateMany() 语句 | |
---|---|
UPDATE people |
db.people.updateMany( |
UPDATE people |
db.people.updateMany( |
For more information on the method and operators used in the examples, see:有关示例中使用的方法和运算符的更多信息,请参阅:
Delete Records删除记录
The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements.下表显示了与从表中删除记录相关的各种SQL语句以及相应的MongoDB语句。
deleteMany() 语句 | |
---|---|
DELETE FROM people |
db.people.deleteMany( { status: "D" } ) |
DELETE FROM people |
db.people.deleteMany({}) |
For more information, see 有关详细信息,请参阅db.collection.deleteMany()
.db.collection.deleteMany()
。
See also: 另请参阅:
Further Reading进一步阅读
If you are considering migrating your SQL application to MongoDB, download the MongoDB Application Modernization Guide如果您正在考虑将SQL应用程序迁移到MongoDB,请下载MongoDB应用程序现代化指南.
。
The download includes the following resources:下载包括以下资源:
Presentation on the methodology of data modeling with MongoDB介绍使用MongoDB进行数据建模的方法White paper covering best practices and considerations for migrating to MongoDB from an RDBMS data model白皮书介绍了从RDBMS数据模型迁移到MongoDB的最佳实践和注意事项Reference MongoDB schema with its RDBMS equivalent参考MongoDB模式及其RDBMS等价物Application Modernization scorecard应用程序现代化记分卡