mongodb 和 mysql 的对照
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.
Executables
The following table presents the MySQL/Oracle executables and the corresponding MongoDB executables.
| MySQL/Oracle | MongoDB | |
|---|---|---|
| Database Server | mysqld/oracle | mongod |
| Database Client | mysql/sqlplus | mongo |
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 framework |
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 | Reference |
|---|---|---|
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")
|
See insert() andcreateCollection()for more information. |
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( |
See the Data Modeling Considerations for MongoDB Applications,update(), and $set for more information on changing the structure of documents in a collection. |
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( |
See Data Modeling Considerations for MongoDB Applications,update(), and $unsetfor more information on changing the structure of documents in a collection. |
CREATE INDEX idx_user_id_asc |
db.users.ensureIndex( { user_id: 1 } )
|
See ensureIndex()and indexes for more information. |
CREATE INDEX |
db.users.ensureIndex( { user_id: 1, age: -1 } )
|
See ensureIndex()and indexes for more information. |
DROP TABLE users |
db.users.drop() |
See drop() for more information. |
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 | Reference |
|---|---|---|
INSERT INTO users(user_id, |
db.users.insert( {
|
See insert() for more information. |
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 | Reference |
|---|---|---|
SELECT * |
db.users.find() |
See find()for more information. |
SELECT id, user_id, status |
db.users.find( |
See find()for more information. |
SELECT user_id, status |
db.users.find( |
See find()for more information. |
SELECT * |
db.users.find( |
See find()for more information. |
SELECT user_id, status |
db.users.find( |
See find()for more information. |
SELECT * |
db.users.find( |
See find()and $ne for more information. |
SELECT * |
db.users.find( |
See find()and $and for more information. |
SELECT * |
db.users.find( |
See find()and $or for more information. |
SELECT * |
db.users.find( |
See find()and $gt for more information. |
SELECT * |
db.users.find( |
See find()and $lt for more information. |
SELECT * |
db.users.find( |
See find(),$gt, and$lte for more information. |
SELECT * |
db.users.find( |
See find()and $regexfor more information. |
SELECT * |
db.users.find( |
See find()and $regexfor more information. |
SELECT * |
db.users.find( { status: "A" } ).sort( { user_id: 1 } )
|
See find()and sort()for more information. |
SELECT * |
db.users.find( { status: "A" } ).sort( { user_id: -1 } )
|
See find()and sort()for more information. |
SELECT COUNT(*) |
db.users.count() or db.users.find().count() |
See find()and count()for more information. |
SELECT COUNT(user_id) |
db.users.count( { user_id: { $exists: true } } )
or db.users.find( { user_id: { $exists: true } } ).count()
|
See find(),count(), and $existsfor more information. |
SELECT COUNT(*) |
db.users.count( { age: { $gt: 30 } } )
or db.users.find( { age: { $gt: 30 } } ).count()
|
See find(),count(), and $gt for more information. |
SELECT DISTINCT(status) |
db.users.distinct( "status" ) |
See find()anddistinct()for more information. |
SELECT * |
db.users.findOne() or db.users.find().limit(1) |
See find(),findOne(), and limit()for more information. |
SELECT * |
db.users.find().limit(5).skip(10) |
See find(),limit(), and skip()for more information. |
EXPLAIN SELECT * |
db.users.find( { status: "A" } ).explain()
|
See find()andexplain()for more information. |
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 | Reference |
|---|---|---|
UPDATE users |
db.users.update( |
See update(), $gt, and $set for more information. |
UPDATE users |
db.users.update( |
See update(), $inc, and $set for more information. |
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 | Reference |
|---|---|---|
DELETE FROM users |
db.users.remove( { status: "D" } )
|
See remove() for more information. |
DELETE FROM users |
db.users.remove( ) |
See remove() for more information. |
mongodb 和 mysql 的对照的更多相关文章
- MongoDB与Mysql常用命令解释
原文 本文旨在介绍MongoDB,Mysql的常用命令:将MongoDB 和传统的关系型数据库的常用命令对照起来学习,更加便于记忆和理解. MongoDB是由数据库(database/reposito ...
- mongodb与mysql命令对比
mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...
- MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)
MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...
- MongoDB 和 mySql 的关系
1. mysql 和 MongoDb MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库. ...
- java mysql 数据类型对照
java mysql 数据类型对照 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lang. ...
- MongoDB和MySQL的区别
http://www.cnblogs.com/caihuafeng/p/5494336.html MongoDB(文档型数据库):提供可扩展的高性能数据存储 一. 1.基于分布式文件存储 2.高负载情 ...
- 【译】MongoDb vs Mysql—以NodeJs为例
亲爱的读者,您可能想知道为什么要写关于MongoDb和MySql这篇文章.那是因为我与NodeJs开发人员讨论在应用程序中使用哪种数据存储作为主要的数据存储方式. 我看过很多评论都在争论这个问题. 有 ...
- MongoDB与MySQL的插入、查询性能测试
1.1 MongoDB的简单介绍 在当今的数据库市场上,MySQL无疑是占有一席之地的.作为一个开源的关系型数据库,MySQL被大量应用在各大网站后台中,承担着信息存储的重要作用.2009年,甲骨文 ...
- mongodb postgresql mysql jsonb对比
mongodb pg mysql jsonb对比 http://erthalion.info/2017/12/21/advanced-json-benchmarks/ 使用禁用jsonb列的压缩 AL ...
随机推荐
- Sql 行转列问题总结
行转列问题总结 1.行转列 ---1.最简单的行转列/* 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物 ...
- 利用JDK的中Proxy动态代理实现Spring的AOP技术
首先给出设计模式静态代理与动态代理的学习: http://layznet.iteye.com/blog/1182924 讲的不错 然后我们实现AOP 就要求我们对委托的所有方法的调用实现拦截 代理 ...
- python接口的调用方法
第一个图灵机器人接口实例: #!/usr/bin/env python # -*- coding: utf_8 -*- import urllib2,urllib import json import ...
- ElasticSearch(ES)和solr的关系和区别
可以参考这篇文章:http://www.cnblogs.com/chowmin/articles/4629220.html Solr 2004年诞生(当时是Solar). ElasticSearch ...
- easyui 删除数据表格
1 最直接的方法: 返回的数据格式 Object rows:Array[3] 0:Object 1:Object 2:Object length:3 __proto__:A ...
- JDK环境变量安装正确还报错的情况解决方案
环境变量 JAVA_HOME , PATH 都已配置完毕,并且 java -version也显示正确如下 E:\apache-tomcat-8.0.30-windows-x64\apache-tomc ...
- Lua 栈中元素的位置
Lua与C.C#等的交互是通过栈来实现的,每次插入元素都是放在栈顶(top),至于元素的index,可以使用正数和负数两种方式, 如取栈底开始至第index个元素 -index = gettop - ...
- phalcon: crypt-encrypt/decrypt用法
phalcon:crypt加密与解密 可以在入口文件index.php进行配置,也可以不配置: $di->set('crypt', function(){ $crypt = new \Phalc ...
- virtualBox虚拟机安装与主机互访和实现上网配置
1.到官方网下载vitualBox虚拟机,目前最新版本5.0,并安装. https://www.virtualbox.org/ 2.下载安装CentOS系统安装文件 本文采用CentOS-6.5-i3 ...
- bootstrap--双日历插件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...