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 ...
随机推荐
- SynchronizationContext的研究之一(非WPF及Forms)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- jquery使用技巧
1. 禁用右键点击(Disable right-click) <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- 百度之星Astar2016 Round2A
All X 等比数列求和一下 A/B MOD C = A MOD (B*C) / B 或者分治一下 Sitting in Line 状压+拓扑dp dp(i, j)表示当前二进制状态为j,当前状态的 ...
- Swift语言学习之学习资源
(1) http://swift.sh (2) Let's Swift – WRITE THE CODE. CHANGE THE WORLD. http://letsswift.com (3)http ...
- PHP中file_put_contents追加和换行
在PHP的一些应用中需要写日志或者记录一些信息,这样的话.可以使用fopen(),fwrite()以及 fclose()这些进行操作.也可以简单的使用file_get_contents()和file_ ...
- mysql连接字符串
MySQL中 concat 函数使用方法:CONCAT(str1,str2,…)
- php 注入
SELECT * FROM `users` WHERE name = 'a\'b\'d' LIMIT 0 , 30 这个是有结果的,运行正确的,和一般想的不一样,单引号里面可以套单引号,只要里面的单引 ...
- samtools常用命令详解(转)
转自:samtools常用命令详解 samtools的说明文档:http://samtools.sourceforge.net/samtools.shtml samtools是一个用于操作sam和ba ...
- 使用Objective-C的文档生成工具
前言 做项目的人多了,就需要文档了.今天开始尝试写一些项目文档.但是就源代码来说,文档最好和源码在一起,这样更新起来更加方便和顺手.象Java语言本身就自带javadoc命令,可以从源码中抽取文档.今 ...
- 【CodeVS 1004】四子连棋
http://blog.csdn.net/u013598409/article/details/43924465 相比于一年半前,代码的掌控能力强了许多. #include <cstdio> ...