有关关系型数据库跟Mongod的语法对比

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 $lookup, embedded documents

primary key

Specify any unique column or column combination as primary key.

primary key

In MongoDB, the primary key is automatically set to the _idfield.

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 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
CREATE TABLE people (
id MEDIUMINT NOT NULL
AUTO_INCREMENT,
user_id Varchar(30),
age Number,
status char(1),
PRIMARY KEY (id)
)

Implicitly created on first insertOne() or insertMany()operation. The primary key _id is automatically added if _id field is not specified.

db.people.insertOne( {
user_id: "abc123",
age: 55,
status: "A"
} )

However, you can also explicitly create a collection:

db.createCollection("people")
ALTER TABLE people
ADD join_date DATETIME

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, updateMany() operations can add fields to existing documents using the $set operator.

db.people.updateMany(
{ },
{ $set: { join_date: new Date() } }
)
ALTER TABLE people
DROP COLUMN join_date

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, updateMany() operations can remove fields from documents using the $unset operator.

db.people.updateMany(
{ },
{ $unset: { "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()

For more information, 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
INSERT INTO people(user_id,
age,
status)
VALUES ("bcd001",
45,
"A")
db.people.insertOne(
{ user_id: "bcd001", age: 45, status: "A" }
)

For more information, see db.collection.insertOne().

Select

The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.

NOTE

The find() method always includes the _id field in the returned documents unless specifically excluded through projection. Some of the SQL queries below may include an _id field to reflect this, even if the field is not included in the corresponding find() query.

SQL SELECT Statements MongoDB find() Statements
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
db.people.distinct( "status" )
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()

For more information, 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
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 } }
)

For more information, see db.collection.updateMany()$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 deleteMany() Statements
DELETE FROM people
WHERE status = "D"
db.people.deleteMany( { status: "D" } )
DELETE FROM people
db.people.deleteMany({})

For more information, see db.collection.deleteMany().

21.SQL to MongoDB Mapping Chart-官方文档摘录的更多相关文章

  1. Cocos Creator 加载和切换场景(官方文档摘录)

    Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...

  2. SQL to MongoDB Mapping Chart

    http://docs.mongodb.org/manual/reference/sql-comparison/ In addition to the charts that follow, you ...

  3. ng的概念层次(官方文档摘录)

    官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...

  4. Cocos Creator 生命周期回调(官方文档摘录)

    Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...

  5. Cocos Creator 使用计时器(官方文档摘录)

    在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...

  6. angular 模板语法(官方文档摘录)

    https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...

  7. Spring 4 官方文档学习(十二)View技术

    关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...

  8. mongodb 语句和SQL语句对应(SQL to Aggregation Mapping Chart)

    SQL to Aggregation Mapping Chart https://docs.mongodb.com/manual/reference/sql-aggregation-compariso ...

  9. Spark SQL 官方文档-中文翻译

    Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...

随机推荐

  1. 基于Ambari构建自己的大数据平台产品

    目前市场上常见的企业级大数据平台型的产品主流的有两个,一个是Cloudera公司推出的CDH,一个是Hortonworks公司推出的一套HDP,其中HDP是以开源的Ambari作为一个管理监控工具,C ...

  2. div允许用户输入

    主要是用到contenteditable属性,就可以用div让用户输入了 <div id="guo" style="width:500px; height:200p ...

  3. 简单好用的包管理器 brew

    Homebrew 是什么? macOS 上的包管理器,相当于 Debian 系的 apt-get ,或者是 Redhat 系的 yum . Homebrew 有什么用? 帮你安装一些系统默认没有安装但 ...

  4. Android应用双击返回键退出

    @Override public void onBackPressed() { // TODO 退出提示 if (System.currentTimeMillis() - mExitTime > ...

  5. BI入门基础知识-1

    基本概念 ODS---ODS(Operational-Data-Store)是数据仓库体系结构中的一个可选部分,ODS具备数据仓库的部分特征和OLTP系统的部分特征,它是“面向主题的.集成的.当前或接 ...

  6. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

  7. Linux中安装配置hadoop集群

    一. 简介 参考了网上许多教程,最终把hadoop在ubuntu14.04中安装配置成功.下面就把详细的安装步骤叙述一下.我所使用的环境:两台ubuntu 14.04 64位的台式机,hadoop选择 ...

  8. Spring Boot简化了基于Spring的应用开发

    Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的.产品级别的Spring应用. Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可 ...

  9. ThinkPHP种where的使用(_logic and _complex)的使用实例

    1.对于thinkphp中的 and ,or 等复合型的查询,我要正确的使用相关的方法. a.实例 b.实例

  10. 《linux系统及其编程》实验课记录(四)

    实验4:组织目录和文件 实验目标: 熟悉几个基本的操作系统文件和目录的命令的功能.语法和用法, 整理出一个更有条理的主目录,每个文件都位于恰当的子目录. 实验背景: 你的主目录中已经积压了一些文件,你 ...