SQL 到 MongoDB 的映射表
In addition to the charts that follow, you might want to consider the 常见问题解答 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 | |
table | |
row | |
column | |
索引(index) | |
table joins |
|
主键 Specify any unique column or column combination as primary key. | |
aggregation (e.g. group by) | 聚合管道 See the SQL 聚合映射图表. |
SELECT INTO NEW_TABLE | See the SQL 聚合映射图表. |
MERGE INTO TABLE | See the SQL 聚合映射图表. |
UNION ALL | |
事务 | For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases instead of multi-document transactions. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions. |
Executables
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
people
.The MongoDB examples assume a collection named
people
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 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Implicitly created on first
However, you can also explicitly create a collection:
| ||||||||||||||
| 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,
| ||||||||||||||
| 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,
| ||||||||||||||
|
| ||||||||||||||
|
| ||||||||||||||
|
|
For more information on the methods and operators used, see:
另请参阅:
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
有关更多信息,请参阅 db.collection.insertOne()
。
另请参阅:
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
| ||||||||
|
-或-
| ||||||||
|
-或-
| ||||||||
|
| ||||||||
|
| ||||||||
|
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
另请参阅:
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 | |||||||
---|---|---|---|---|---|---|---|---|
|
| |||||||
|
|
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 Delete Statements | MongoDB deleteMany() Statements | |||
---|---|---|---|---|
|
| |||
|
|
有关更多信息,请参阅 db.collection.deleteMany()
。
另请参阅:
深入阅读
If you are considering migrating your SQL application to MongoDB, download the MongoDB Application Modernization Guide.
下载内容包括以下资源:
介绍使用 MongoDB 进行数据建模的方法
白皮书介绍了从 RDBMS 数据模型迁移到 MongoDB 的最佳实践和注意事项
引用 MongoDB 模式及其 RDBMS 等效模式
应用程序现代化记分卡