SQL to MongoDB Mapping Chart
http://docs.mongodb.org/manual/reference/sql-comparison/
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 | embedded documents and linking |
|
primary key Specify any unique column or column combination as primary key. |
In MongoDB, the primary key is automatically set to the _id field. |
| 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 users.
The MongoDB examples assume a collection named users 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 |
|---|---|
CREATE TABLE users ( |
Implicitly created on first insert() operation. The primary key _id is automatically added if _id field is not specified. db.users.insert( {
However, you can also explicitly create a collection: db.createCollection("users")
|
ALTER TABLE users |
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, update() operations can add fields to existing documents using the $set operator. db.users.update( |
ALTER TABLE users |
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, update() operations can remove fields from documents using the $unset operator. db.users.update( |
CREATE INDEX idx_user_id_asc |
db.users.ensureIndex( { user_id: 1 } )
|
CREATE INDEX |
db.users.ensureIndex( { user_id: 1, age: -1 } )
|
DROP TABLE users |
db.users.drop() |
For more information, see db.collection.insert(), db.createCollection(), db.collection.update(), $set, $unset, db.collection.ensureIndex(), indexes, db.collection.drop(), and 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 insert() Statements |
|---|---|
INSERT INTO users(user_id, |
db.users.insert( |
For more information, see db.collection.insert().
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 |
|---|---|
SELECT * |
db.users.find() |
SELECT id, |
db.users.find( |
SELECT user_id, status |
db.users.find( |
SELECT * |
db.users.find( |
SELECT user_id, status |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( |
SELECT * |
db.users.find( { user_id: /bc/ } )
|
SELECT * |
db.users.find( { user_id: /^bc/ } )
|
SELECT * |
db.users.find( { status: "A" } ).sort( { user_id: 1 } )
|
SELECT * |
db.users.find( { status: "A" } ).sort( { user_id: -1 } )
|
SELECT COUNT(*) |
db.users.count() or db.users.find().count() |
SELECT COUNT(user_id) |
db.users.count( { user_id: { $exists: true } } )
or db.users.find( { user_id: { $exists: true } } ).count()
|
SELECT COUNT(*) |
db.users.count( { age: { $gt: 30 } } )
or db.users.find( { age: { $gt: 30 } } ).count()
|
SELECT DISTINCT(status) |
db.users.distinct( "status" ) |
SELECT * |
db.users.findOne() or db.users.find().limit(1) |
SELECT * |
db.users.find().limit(5).skip(10) |
EXPLAIN SELECT * |
db.users.find( { status: "A" } ).explain()
|
For more information, see db.collection.find(), db.collection.distinct(), db.collection.findOne(), $ne $and, $or, $gt, $lt, $exists, $lte, $regex, limit(), skip(), explain(), sort(), and 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 update() Statements |
|---|---|
UPDATE users |
db.users.update( |
UPDATE users |
db.users.update( |
For more information, see db.collection.update(), $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 remove() Statements |
|---|---|
DELETE FROM users |
db.users.remove( { status: "D" } )
|
DELETE FROM users |
db.users.remove({})
|
For more information, see db.collection.remove().
SQL to MongoDB Mapping Chart的更多相关文章
- mongodb 语句和SQL语句对应(SQL to Aggregation Mapping Chart)
SQL to Aggregation Mapping Chart https://docs.mongodb.com/manual/reference/sql-aggregation-compariso ...
- 21.SQL to MongoDB Mapping Chart-官方文档摘录
有关关系型数据库跟Mongod的语法对比 In addition to the charts that follow, you might want to consider the Frequentl ...
- 从 SQL 到 MongoDB,这一篇就够了
很多开发者首次接触数据库(通常是在高校课堂)的概念,或者说接触第一个数据库,通常是 SQL 数据库,而现在,NoSQL 数据库后来居上,很多原 SQL 数据的使用者难免有转向 NoSQL 的需求.而作 ...
- 使用SQL访问MongoDB
使用SQL访问MongoDB 简介 使用SQL访问MongoDB有多种解决方案,就我所知的,除了今天要介绍的MongoDB Connector for BI外,还有Studio 3T,但后者只有在企业 ...
- SQL与Mongodb聚合的对应关系(举例说明)
SQL中的聚合函数和Mongodb中的管道相互对应的关系: WHERE $match GROUP BY $group HAVING $match SELECT $project ORDER BY $s ...
- MongoDB学习第七篇 --- sql和mongodb对比
一.术语和概念的对比 SQL MongoDB database database row document or BSON document column field index index ...
- MongoDB数据库常用SQL命令 — MongoDB可视化工具Robo 3T
1.db.collection.updateMany() 修改集合中的多个文档. db.getCollection('user').find({"pId":"3332a5 ...
- mongodb(基础用法)
驱动和客户端库 https://mongodb-documentation.readthedocs.org/en/latest/ecosystem/drivers.html#id2 https://m ...
- Mongodb 和 普通数据库 各种属性 和语句 的对应
SQL to MongoDB Mapping Chart In addition to the charts that follow, you might want to consider the F ...
随机推荐
- Linux常用命令分类
目录:相对路径和绝对路径. 绝对路径:路径的写法[一定由根目录 / 写起],例如: /usr/share/doc 这个目录. 相对路径:路径的写法[不是由 / 写起],例如由 /usr/share/d ...
- CentOS 7 配置 Nginx 正向代理 http、https 最详解
手头项目中有使用到 nginx,因为使用的三方云服务器,想上外网需要购买外网IP的,可是有些需要用到外网却不常用的主机也挂个外网IP有点浪费了,便想使用nginx的反向代理来实现多台内网服务器使用一台 ...
- [SDOI2010]外星千足虫(高斯消元)
高斯消元裸题... 方法一:暴力,O(2^n)20分 方法二:直接Gauss,加点玄学技巧搞得好的话70分 方法三:使用bitset优化,复杂度:$O(\frac{n^3}{ω})$ 不会的同学看一下 ...
- Web前端汇总
http://www.cnblogs.com/bigboyLin/p/5272902.html HTML/CSS部分 1.什么是盒子模型? 在网页中,一个元素占有空间的大小由几个部分构成,其中包括 ...
- 解决:阿里云ECS上启动tomcat后,第一次访问时间特别长
Re在ECS上启动tomcat后,第一次访问时间特别长 2017-04-25 10:16:04 INFO com.world.socket.ServerSocketListener 25- ...
- [ CERC 2014 ] Vocabulary
\(\\\) \(Description\) 给出三个长度分别为 \(lenA,lenB,lenC\) 的三个字符串 \(A,B,C\) ,其中字符集只包括所有小写字母以及 \(?\) 号. 现在将所 ...
- css边框样式、边框配色、边框阴影、边框圆角、图片边框
边框样式 点线式边框 破折线式边框 直线式边框 双线式边框 槽线式边框 脊线式边框 内嵌效果的边框 突起效果的边框 <div style="width: 300px; height: ...
- oracle添加联合主键
1 alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为 ...
- 世界上最受欢迎的10个Linux发行版
帮助新的Linux用户在越来越多的Linux发行版中选择最合适的操作系统,是创建这个网页的原因.它列出了迄今为止最流行的10个Linux发行版(另外增加的是FreeBSD,到目前为止最为流行的BSD系 ...
- POJ_2387_最短路
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46859 Accepted ...