Mongodb 和 普通数据库 各种属性 和语句 的对应
SQL to MongoDB Mapping Chart
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.createIndex( { user_id: 1 } )
|
CREATE INDEX |
db.users.createIndex( { 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.createIndex(), 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 Aggregation Mapping Chart
The aggregation pipeline allows MongoDB to provide native aggregation capabilities that corresponds to many common data aggregation operations in SQL.
The following table provides an overview of common SQL aggregation terms, functions, and concepts and the corresponding MongoDB aggregation operators:
| SQL Terms, Functions, and Concepts | MongoDB Aggregation Operators |
|---|---|
| WHERE | $match |
| GROUP BY | $group |
| HAVING | $match |
| SELECT | $project |
| ORDER BY | $sort |
| LIMIT | $limit |
| SUM() | $sum |
| COUNT() | $sum |
| join | No direct corresponding operator; however, the$unwind operator allows for somewhat similar functionality, but with fields embedded within the document. |
Examples
The following table presents a quick reference of SQL aggregation statements and the corresponding MongoDB statements. The examples in the table assume the following conditions:
The SQL examples assume two tables, orders and order_lineitem that join by theorder_lineitem.order_id and the orders.id columns.
The MongoDB examples assume one collection orders that contain documents of the following prototype:
{
cust_id: "abc123",
ord_date: ISODate("2012-11-02T17:04:11.102Z"),
status: 'A',
price: 50,
items: [ { sku: "xxx", qty: 25, price: 1 },
{ sku: "yyy", qty: 25, price: 1 } ]
}
| SQL Example | MongoDB Example | Description |
|---|---|---|
SELECT COUNT(*) AS count |
db.orders.aggregate( [ |
Count all records fromorders |
SELECT SUM(price) AS total |
db.orders.aggregate( [ |
Sum theprice field from orders |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id, sum theprice field. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id, sum theprice field, results sorted by sum. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id,ord_dategrouping, sum the pricefield. Excludes the time portion of the date. |
SELECT cust_id, |
db.orders.aggregate( [ |
For cust_idwith multiple records, return thecust_id and the corresponding record count. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id,ord_dategrouping, sum the pricefield and return only where the sum is greater than 250. Excludes the time portion of the date. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id with status A, sum the pricefield. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id with status A, sum the pricefield and return only where the sum is greater than 250. |
SELECT cust_id, |
db.orders.aggregate( [ |
For each uniquecust_id, sum the corresponding line item qtyfields associated with the orders. |
SELECT COUNT(*) |
db.orders.aggregate( [ |
Mongodb 和 普通数据库 各种属性 和语句 的对应的更多相关文章
- oracle数据库查询日期sql语句(范例)、向已经建好的表格中添加一列属性并向该列添加数值、删除某一列的数据(一整列)
先列上我的数据库表格: c_date(Date格式) date_type(String格式) 2011-01-01 0 2012-03-07 ...
- MySQL、MongoDB、Redis 数据库之间的区别
NoSQL 的全称是 Not Only SQL,也可以理解非关系型的数据库,是一种新型的革命式的数据库设计方式,不过它不是为了取代传统的关系型数据库而被设计的,它们分别代表了不同的数据库设计思路. M ...
- MySQL、MongoDB、Redis 数据库之间的区别与使用(本章迭代更新)
MySQL.MongoDB.Redis 数据库之间的区别与使用 MySQL.MongoDB.Redis 数据库之间的区别与使用(本章迭代更新) update:2019年2月20日 15:21:19(本 ...
- MySQL、MongoDB、Redis数据库Docker镜像制作
MySQL.MongoDB.Redis数据库Docker镜像制作 在多台主机上进行数据库部署时,如果使用传统的MySQL的交互式的安装方式将会重复很多遍.如果做成镜像,那么我们只需要make once ...
- MongoDB和Redis-NoSQL数据库-文档型-内存型
1NoSQL简述 CAP(Consistency,Availabiity,Partitiontolerance)理论告诉我们,一个分布式系统不可能满足一致性,可用性和分区容错性这三个需求,最多只能同时 ...
- MongoDB非关系型数据库开发手册
一:NoSql数据库 什么是NoSQL? NoSQL,指的是非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称. NoSQL用于超 ...
- 第12章—使用NoSQL数据库—使用MongoDB+Jpa操作数据库
使用MongoDB+Jpa操作数据库 SpringData还提供了对多种NoSQL数据库的支持,包括MongoDB;neo4j和redis.他不仅支持自动化的repository,还支持基于模板的数据 ...
- 【数据库】 SQL 常用语句之系统语法
[数据库] SQL 常用语句之系统语法 1. 获取取数据库服务器上所有数据库的名字 SELECT name FROM master.dbo.sysdatabases 2. 获取取数据库服务器上所有非系 ...
- MongoDB(三):数据库操作、集合操作
1. 数据库操作 看完前面的文章,大家应该把环境搭建好了,下面我们就开始学习MongoDB的一些基本操作了. 首先我们要了解的一些要点: MongoDB将数据存储为一个文档,数据结构由键值对(key= ...
随机推荐
- Python天天美味(15) - Python正则表达式操作指南(re使用)(转)
http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...
- pthread_create用法
linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...
- strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签
定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...
- POJ1002487-3279(map)
http://poj.org/problem?id=1002 题意:是说很多公司用了容易记住的电话号码,例如有英文字母的或者是用了很多连字符或没有连字符的.每个电话号码都有标准模式,而为了统计有没有重 ...
- hbase集群在启动的时候找不到JAVA_HOME的问题
hbase集群在启动的时候找不到JAVA_HOME的问题,启动集群的时候报错信息如下: root@master:/usr/local/hbase-/bin# ./start-hbase.sh star ...
- Nginx/Apache日志分析脚本
1,查看apache进程: ps aux | grep httpd | grep -v grep | wc -l 2,查看80端口的tcp连接: netstat -tan | grep "E ...
- Python字符串编码问题
编码问题:Unicode把所有语言都统一到一套编码里,这样就不会再有乱码问题了. ASCII编码和Unicode编码的区别:ASCII编码是1个字节,而Unicode编码通常是2个字节.字母A用ASC ...
- SRM 585 DIV1 L2
记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ...
- 解决git Push时请求username和password,而不是ssh-key验证
转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...
- log file sync
Recently, our application system has updated one app. I receive a email of complain the db server ch ...