MongoDB学习第七篇 --- sql和mongodb对比
一、术语和概念的对比
| SQL | MongoDB |
|---|---|
| database | database |
| 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 _id field. |
| aggregation (e.g. group by) |
aggregation pipeline |
二、对库和表的操作语句对比
| SQL | MongoDB |
|---|---|
CREATE TABLE people ( |
db.people.insertOne( {
也可以用一下语句创建collection db.createCollection("people")
|
ALTER TABLE people |
db.people.updateMany( |
ALTER TABLE people |
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() |
三、insert语句的对比
| SQL INSERT Statements | MongoDB insertOne() Statements |
|---|---|
INSERT INTO people(user_id, |
db.people.insertOne( |
四、select语句的对比
| 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()
|
五、update语句的对比
| SQL Update Statements | MongoDB updateMany() Statements |
|---|---|
UPDATE people |
db.people.updateMany( |
UPDATE people |
db.people.updateMany( |
六、delete语句的对比
| SQL Delete Statements | MongoDB deleteMany() Statements |
|---|---|
DELETE FROM people |
db.people.deleteMany( { status: "D" } )
|
DELETE FROM people |
db.people.deleteMany({})
|
MongoDB学习第七篇 --- sql和mongodb对比的更多相关文章
- MongoDB学习笔记(1):MongoDB的安装和说明
MongoDB学习笔记(1):MongoDB的安装和说明 快速开始 下载地址 官网下载: https://www.mongodb.com/download-center?jmp=nav#communi ...
- MongoDB学习笔记七:管理
[启动和停止MongoDB]『从命令行启动』执行mongod,启动MongoDB服务器.mongod有很多可配置的启动选项:在命令行运行mongod --help可以查看所有选项.一些主要选项如下: ...
- 第七篇 SQL Server安全跨数据库所有权链接
本篇文章是SQL Server安全系列的第七篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
- 【译】第七篇 SQL Server安全跨数据库所有权链接
本篇文章是SQL Server安全系列的第七篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
- 第七篇 SQL Server代理作业活动监视器
本篇文章是SQL Server代理系列的第七篇,详细内容请参考原文 在这一系列的上一篇,你创建并配置SQL Server代理作业.每个作业有一个或多个步骤,可能包含大量的工作流.在这篇文章中,将查看作 ...
- 【译】第七篇 SQL Server代理作业活动监视器
本篇文章是SQL Server代理系列的第七篇,详细内容请参考原文 在这一系列的上一篇,你创建并配置SQL Server代理作业.每个作业有一个或多个步骤,可能包含大量的工作流.在这篇文章中,将查看作 ...
- MongoDB学习【四】—pymongo操作mongodb数据库
一.pymongodb的安装 Python 要连接 MongoDB 需要 MongoDB 驱动,这里我们使用 PyMongo 驱动来连接. pip安装 pip 是一个通用的 Python 包管理工具, ...
- MongoDB学习比较-07 C#驱动操作MongoDB
下载驱动 驱动的下载有两种方式:一种是在C#项目中通过NuGet进行安装,另一种是通过下面的链接:https://github.com/mongodb/mongo-csharp-driver/rele ...
- 数据库系统学习(七)-SQL语言之复杂查询与视图
第七讲 SQL语言之复杂查询与视图 基本内容 子查询 IN与NOT IN谓词子查询 判断某一表达式的值是否在子查询的结构中 非相关子查询 相关子查询 theta some /theta all谓词子查 ...
随机推荐
- 苹果快速的修复了Mac OS High Sierra 上出现了root的漏洞
最近苹果因为Mac最新系统 Mac OS High Sierra 上出现了root的漏洞走上了风口浪尖,不过还好,在一封苹果给科技媒体'9to5 Mac'的回复中得知,苹果在接收到报告之后,立即展开修 ...
- 变位词(0029)-swustoj
变位词(0029)水题 变位词如果两个单词的组成字母完全相同,只是字母的排列顺序不一样,则它们就是变位词,两个单词相同也被认为是变位词.如tea 与eat , nic 与cin, ddc与dcd, a ...
- C语言之猜数游戏
#include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ srand(time(0)); int ...
- ubuntu实时显示网速cpu占用和内存占用率
ubuntu实时显示网速cpu占用和内存占用率 大家在使用ubuntu的时候,有没有想让它实时显示网速,内存占用率,或者cpu占用率呢?现在我就教大家怎么实现,就像下面这样 1. 添加indicato ...
- C#检测获取移动硬盘盘符
最近做一个小工具, C# 对 移动硬盘的检测, var arr = DriveInfo.GetDrives(); 得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型 ...
- leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?
/* Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...
- P1092 虫食算
题目传送:https://www.luogu.org/problem/show?pid=1092 #include <iostream> #include <cstring> ...
- Tp3.2 和 Tp5.0之间的区别
5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别. URL和路由 5.0的URL访问不再支持普通URL模式,路由也不支持正则路由定义,而是全部改为规则路由配合变量规 ...
- Codeforces 869C The Intriguing Obsession
题意:有三种颜色的岛屿各a,b,c座,你可以在上面建桥.联通的点必须满足以下条件:1.颜色不同.2.颜色相同且联通的两个点之间的最短路径为3 其实之用考虑两种颜色的即可,状态转移方程也不难推出:F[i ...
- VSCode jQuery代码提示
在VSCode中,ctrl+`打开终端: 运行命令: npm install @types/jquery --save 最后在项目中会生成: 说明安装成功! 详情可参见:链接