21.SQL to MongoDB Mapping Chart-官方文档摘录
有关关系型数据库跟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. |
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
peoplethat 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 ( |
Implicitly created on first db.people.insertOne( {
However, you can also explicitly create a collection: db.createCollection("people")
|
ALTER TABLE people |
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, db.people.updateMany( |
ALTER TABLE people |
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, 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, 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, |
db.people.insertOne( |
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 * |
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.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, 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 |
db.people.updateMany( |
UPDATE people |
db.people.updateMany( |
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 |
db.people.deleteMany( { status: "D" } )
|
DELETE FROM people |
db.people.deleteMany({})
|
For more information, see db.collection.deleteMany().
21.SQL to MongoDB Mapping Chart-官方文档摘录的更多相关文章
- Cocos Creator 加载和切换场景(官方文档摘录)
Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...
- SQL to MongoDB Mapping Chart
http://docs.mongodb.org/manual/reference/sql-comparison/ In addition to the charts that follow, you ...
- ng的概念层次(官方文档摘录)
官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...
- Cocos Creator 生命周期回调(官方文档摘录)
Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...
- Cocos Creator 使用计时器(官方文档摘录)
在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...
- angular 模板语法(官方文档摘录)
https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...
- Spring 4 官方文档学习(十二)View技术
关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...
- mongodb 语句和SQL语句对应(SQL to Aggregation Mapping Chart)
SQL to Aggregation Mapping Chart https://docs.mongodb.com/manual/reference/sql-aggregation-compariso ...
- Spark SQL 官方文档-中文翻译
Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...
随机推荐
- eclipse生成export生成jar详解
使用eclipse打jar包可能还有很多人不是很了解,今天特意测试整理一番. 打jar包有3种形式 JAR file JAR Javadoc ja ...
- python walk函数
os.walk方法 import os for i in os.walk(r'C:\Users\jack\Desktop\test\3_语文语文版七年级上册\1_一单元'): print(i[0]) ...
- shell监控脚本,不考虑多用户情况
#!/bin/bash CheckProcess() { if [ "$1" = "" ]; then fi PROCESS_NUM=`ps -ef | gre ...
- ExecuteNonQuery和ExecuteScalar的区别
ExecuteNonQuery 针对 Connection 执行 SQL 语句并返回受影响的行数. 返回值 受影响的行数. 备注 您可以使用 ExecuteNonQuery 来执行目录操作(例如查 ...
- 一个性能较好的JVM参数配置(转)
一个性能较好的web服务器jvm参数配置: -server//服务器模式-Xmx2g //JVM最大允许分配的堆内存,按需分配-Xms2g //JVM初始分配的堆内存,一般和Xmx配置成一样以避免每次 ...
- Java Spring Boot: Unable to determine jdbc url from datasource
如果你和我一样从github或码云上下载了一个几年前别人写的demo代码,想用来做学习用.编译的时候遇到下面这样的错误,然后死命上网查各种方案,百试不灵.试尽了各种方案,就是还连接不上数据库.你可以试 ...
- EasyUI DataGrid合并单元
<table id="tt"></table> $('#tt').datagrid({ title:'Merge Cells', iconC ...
- 【cf492】D. Vanya and Computer Game(二分)
http://codeforces.com/contest/492/problem/D 有时候感觉人sb还是sb,为什么题目都看不清楚? x per second, y per second... 于 ...
- Linux命令之乐--test
官方文档: help test File operators: -a FILE True if file exists. -b FILE True if file is block special. ...
- delay()延迟
jquery的delay()方法: delay() 方法对队列中的下一项的执行设置延迟. 封装方法: $.fn.fadeInWithDelay = function(){ var delay = 0; ...