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 ...
随机推荐
- lazyload懒加载的使用
1.引用<script src="http://a.tbcdn.cn/apps/baron/js/??lib/tmm/tmm.js,lib/lazyload/lazyload.js?2 ...
- nyoj 1058部分和问题(DFS)
部分和问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使它们的和恰好为K. 输入 首先, ...
- JMS【三】--ActiveMQ简单的HelloWorld实例
第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...
- WdatePicker.js 日历点击时,触发自定义方法 ,可以调用自己的函数。
问题: 在选择日期后,没有提交按钮,得到日期后,就可以把日期传到后台,然后就可以得到数据. 方法: 在input 标签中加入onfocus ,就可以了. wdatePicker();可以自定义事件函数 ...
- 细心看完这篇文章,刷新对Javascript Prototype的理解
var person={name:'ninja'}; person.prototype.sayName=function(){ return this.name; } 分析上面这段代码,看看有没有问题 ...
- 阿里商业评论 | 互联网POI数据及其在营销中的应用
阿里商业评论 | 互联网POI数据及其在营销中的应用 时间 2014-11-05 10:40:50 阿里研究院 原文 http://www.aliresearch.com/index.php?m- ...
- Windows_cmd_命令
1. netstat -ano 查看端口占用情况 netstat -anp // 命令来查看一下,Linux系统是否在监听 3306 这个端口号 2.
- C++——友元、异常和其他
一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...
- Android事件传递机制(转)
Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和多指操作.所有这些都构成了Android中的事件响应.总的来说,所有的事件都 ...
- Image Cropper+java实现截图工具
首先,请移步http://jquery-plugins.net/image-cropper-jquery-image-cropping-plugin下载iamge cropper的有关js文件及css ...