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.


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 (
    id MEDIUMINT NOT NULL
        AUTO_INCREMENT,
    user_id Varchar(30),
    age Number,
    status char(1),
    PRIMARY KEY (id)
)

 直接执行insert操作即可,如果表不存在,自动创建

db.people.insertOne( {
    user_id: "abc123",
    age: 55,
    status: "A"
 } )

也可以用一下语句创建collection

db.createCollection("people")
ALTER TABLE people
ADD join_date DATETIME
db.people.updateMany(
    { },
    { $set: { join_date: new Date() } }
)
ALTER TABLE people
DROP COLUMN join_date
db.people.updateMany(
    { },
    { $unset: { "join_date": "" } }
)
CREATE INDEX idx_user_id_asc
ON people(user_id)
db.people.createIndex( { user_id: 1 } )
CREATE INDEX
       idx_user_id_asc_age_desc
ON people(user_id, age DESC)
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,
                  age,
                  status)
VALUES ("bcd001",
        45,
        "A")
db.people.insertOne(
   { user_id: "bcd001", age: 45, status: "A" }
)

四、select语句的对比

SQL SELECT Statements MongoDB find() Statements
SELECT *
FROM people
db.people.find()
SELECT id,
       user_id,
       status
FROM people
db.people.find(
    { },
    { user_id: 1, status: 1 }
)
SELECT user_id, status
FROM people
db.people.find(
    { },
    { user_id: 1, status: 1, _id: 0 }
)
SELECT *
FROM people
WHERE status = "A"
db.people.find(
    { status: "A" }
)
SELECT user_id, status
FROM people
WHERE status = "A"
db.people.find(
    { status: "A" },
    { user_id: 1, status: 1, _id: 0 }
)
SELECT *
FROM people
WHERE status != "A"
db.people.find(
    { status: { $ne: "A" } }
)
SELECT *
FROM people
WHERE status = "A"
AND age = 50
db.people.find(
    { status: "A",
      age: 50 }
)
SELECT *
FROM people
WHERE status = "A"
OR age = 50
db.people.find(
    { $or: [ { status: "A" } ,
             { age: 50 } ] }
)
SELECT *
FROM people
WHERE age > 25
db.people.find(
    { age: { $gt: 25 } }
)
SELECT *
FROM people
WHERE age < 25
db.people.find(
   { age: { $lt: 25 } }
)
SELECT *
FROM people
WHERE age > 25
AND   age <= 50
db.people.find(
   { age: { $gt: 25, $lte: 50 } }
)
SELECT *
FROM people
WHERE user_id like "%bc%"
db.people.find( { user_id: /bc/ } )

-or-

db.people.find( { user_id: { $regex: /bc/ } } )
SELECT *
FROM people
WHERE user_id like "bc%"
db.people.find( { user_id: /^bc/ } )

-or-

db.people.find( { user_id: { $regex: /^bc/ } } )
SELECT *
FROM people
WHERE status = "A"
ORDER BY user_id ASC
db.people.find( { status: "A" } ).sort( { user_id: 1 } )
SELECT *
FROM people
WHERE status = "A"
ORDER BY user_id DESC
db.people.find( { status: "A" } ).sort( { user_id: -1 } )
SELECT COUNT(*)
FROM people
db.people.count()

or

db.people.find().count()
SELECT COUNT(user_id)
FROM people
db.people.count( { user_id: { $exists: true } } )

or

db.people.find( { user_id: { $exists: true } } ).count()
SELECT COUNT(*)
FROM people
WHERE age > 30
db.people.count( { age: { $gt: 30 } } )

or

db.people.find( { age: { $gt: 30 } } ).count()
SELECT DISTINCT(status)
FROM people
db.people.distinct( "status" )
SELECT *
FROM people
LIMIT 1
db.people.findOne()

or

db.people.find().limit(1)
SELECT *
FROM people
LIMIT 5
SKIP 10
db.people.find().limit(5).skip(10)
EXPLAIN SELECT *
FROM people
WHERE status = "A"
db.people.find( { status: "A" } ).explain()

五、update语句的对比

SQL Update Statements MongoDB updateMany() Statements
UPDATE people
SET status = "C"
WHERE age > 25
db.people.updateMany(
   { age: { $gt: 25 } },
   { $set: { status: "C" } }
)
UPDATE people
SET age = age + 3
WHERE status = "A"
db.people.updateMany(
   { status: "A" } ,
   { $inc: { age: 3 } }
)

六、delete语句的对比

SQL Delete Statements MongoDB deleteMany() Statements
DELETE FROM people
WHERE status = "D"
db.people.deleteMany( { status: "D" } )
DELETE FROM people
db.people.deleteMany({})
时间: 2024-10-12 13:43:40

MongoDB学习第七篇 --- sql和mongodb对比的相关文章

python学习 第七篇 sql优化

查询单条语句:用来获取用户列表和单个用户 >>> fields=["id","name","name_cn","email","mobile"]                 >>> sql="select %s from users where name ='admin'" % ','.join(fields) >>> sql &

渗透学习笔记--基础篇--sql注入(字符型)

环境:dvwa1.7数据库:mysql前置知识:sql语句(Click me)      在进行sql注入前,我们先熟悉熟悉select语句.一.打开我们的sql终端 二.进入之后可以看到有mysql>我们输入sql语句,即可返回我们想要的结果,注意分号哟!我们使用的dvwa,在我们前几章设置的时候,会在数据库中生成一个dvwa的database:这里我们使用它来进行我们的select 语句:(1)使用dvwa数据库use dvwa;(2)在users表里查询用户名为'admin'的所有信息se

第七篇 SQL Server安全跨数据库所有权链接

本篇文章是SQL Server安全系列的第七篇,详细内容请参考原文. Relational databases are used in an amazing variety of applications with connections from a dizzying array of clients over widely distributed networks,特别是互联网,使得数据几乎向任何人,任何地方开放.数据库可以包含相当大部分的人类知识,包括高度敏感的个人信息和关键数据.数据库的

第七篇 SQL Server代理作业活动监视器

本篇文章是SQL Server代理系列的第七篇,详细内容请参考原文 在这系列的前几篇,你创建并配置SQL Server代理作业.每个作业有一个或多个步骤,可能包含大量的工作流.在这篇文章中,将查看作业活动监视器.作业活动监视器是系统管理工具来运行工作,查看作业历史记录,并启用/禁用作业.本文同样会回顾一些作业活动监视器调用的存储过程,你可以直接调用过程做你自己的自定义任务监控.SQL Server代理作业活动监视器SQL Server代理作业活动监视器的主要机制是监视作业运行,以及查看代理已运行

MongoDB学习比较-07 C#驱动操作MongoDB

下载驱动 驱动的下载有两种方式:一种是在C#项目中通过NuGet进行安装,另一种是通过下面的链接:https://github.com/mongodb/mongo-csharp-driver/releases 直接下载msi进行安装或zip压缩包.不管哪种方式,其主要的目的都是获取两个dll文件:MongoDB.Bson.dll.MongoDB.Driver.dll.这是在程序中需要引用的两个类库文件. .NET版本要求 目前最新版的C#驱动是1.9.2,是在 .NET3.5的基础上构建的,所以

MongoDB学习笔记&lt;七&gt;

继续MongoDB的学习 1.导出数据(中断其他操作) 把数据库test中的books集合导出 在终端下进行操作 mongoexport -d test -c books -o /home/hadoop/Desktop/books.json 2.导入数据(中断其他操作) 也是在终端下进行操作 mongoimport --db test --collection books --file /home/hadoop/Desktop/books.json 3.运行时备份mongodump 终端下进行操

MongoDB学习第四篇 --- Query操作

一.shell执行mongodb查询(简单json数据结构) 查询所有:db.inventory.find() 按条件查询:db.inventory.find( { status: "D" } ) in条件查询:db.inventory.find( { status: { $in: [ "A", "D" ] } } ) and和范围条件查询:db.inventory.find( { status: "A", qty: { $l

MongoDB学习第无篇 --- Update操作

数据如下: db.inventory.insertMany( [ { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" }, { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { it

渗透学习笔记--基础篇--sql注入(数字型)

环境:dvwa 1.7数据库:mysql dvwa的安全等级:medium 一.分析和查找注入点(1)知识点回顾如果上一篇有好好读过的同学应该知道,我们上一篇遇到的字符型注入.也即是通过Get或者Post方式传进去的数据被单引号或者双引号包裹住.如果我们想要注入自己的payload(有效载荷)的话,则我们必须先闭合前面的单引号或者双引号,否则我们的数据始终会被当做成字符串来处理. 这种类型的注入点称为字符型注入点. (2)这次我们的把防御等级提升了一个层次,来逐步加强我们手工注入的能力以及开更多