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 ...
随机推荐
- bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】
用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...
- bzoj 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场【bfs】
不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!! 是我看不懂人话还是翻译不说人话= = 把所有格子按值排个序,bfs扩展打标记即可 #includ ...
- C#的装箱与拆箱与基本类型
装箱:值类型转换为对象类型, 实例: int val = 8; object obj = val;//整型数据转换为了对象类型(装箱) 拆箱:之前由值类型转换而来的对象类型再转回值类型, 实例: in ...
- 388 Longest Absolute File Path 最长的绝对文件路径
详见:https://leetcode.com/problems/longest-absolute-file-path/description/ C++: class Solution { publi ...
- sql server 行转列 要注意的问题 pivot
select * from ( select mvqr.VoteQuestionId,mvqr.AnswerSolution from JY_MemberVoteQuestionRef as ...
- Java多线程——线程之间的协作
Java多线程——线程之间的协作 摘要:本文主要学习多线程之间是如何协作的,以及如何使用wait()方法与notify()/notifyAll()方法. 部分内容来自以下博客: https://www ...
- CF817B Makes And The Product
思路: 模拟,数学. 实现: #include <iostream> #include <cstdio> #include <algorithm> using na ...
- IPython、Notebook、qtconsole使用教程
IPython.Notebook.qtconsole使用教程 上一篇为Python,IPython,qtconsole,Notebook,Jupyter快速安装教程 1. 使用IPython 自动补全 ...
- PHP常见问题总结
1.为什么会出现这种情况?端口什么的都设置正确了. 解决方法: 请将本机的IIS服务关闭,开启Apache服务.IIS服务的关闭方法可参见 https://jingyan.baidu.com/arti ...
- Flask框架 之路由
一.视图函数路由规则 from flask import Flask, redirect, url_for # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录 ...