SQL和MongoDB对比

关系型数据库如MySQL和非关系型数据库MongoDB的对应关系:

SQL

MongoDB

database

database

table

collection

row

document or Bson document

column

field

index

index

table joins

$lookup

primary key

primary key

指定任何唯一的列或列组合作为主键

主键会自动设置为_id字段

aggregation (group by)

aggregation pipeline

select into table

$out

merge into table

$merge

union all

$unionWith

transactions

transacctions

Example

CREATE TABLE people (
    id MEDIUMINT NOT NULL
        AUTO_INCREMENT,
    user_id Varchar(30),
    age Number,
    status char(1),
    PRIMARY KEY (id)
)

Implicitly created on first insertOne() or insertMany() operation. The primary key _id is automatically added if _id field is not specified.

db.people.insertOne( {
    user_id: "abc123",
    age: 55,
    status: "A"
 } )
ALTER TABLE people
DROP COLUMN join_date

使用$set添加field

db.people.updateMany(
    { },
    { $set: { join_date: new Date() } }
)
ALTER TABLE people
DROP COLUMN join_date

使用$unset删除field

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)

-1表示降序

db.people.createIndex( { user_id: 1, age: -1 } )
DROP TABLE people
db.people.drop()
INSERT INTO people(user_id,
                  age,
                  status)
VALUES ("bcd001",
        45,
        "A")
db.people.insertOne(
   { user_id: "bcd001", age: 45, status: "A" }
)
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
//0表示不显示改行
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 OR age = 50
db.people.find({$or:[{status:"A"},{age:50}]})
SELECT * FROM people WHERE age >= 25
db.people.find({age:{$gte: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/} )
db.people.find({user_id:{$regex:/bc/}})
SELECT * FROM people WHERE user_id LIKE "%bc"
db.people.find({user_id:/^bc/})
db.people.find({user_id:{$regex:/^bc/}})
SELECT * FROM people WHERE status = "A" ORDER BY user_id ASC
SELECT * FROM people WHERE status = "A" ORDER BY user_id DESC
db.people.find({status:"A"}).sort({user_id:1})
db.people.find({status:"A"}).sort({user_id:-1})
SELECT count(*) FROM people
db.people.count() or 
db.people.find().count() or 
db.people.count({user_id:{$exists:true}})
SELECT count(*) FROM people WHERE age>30
db.people.count({age:{$gt:30}})
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()

附MongoDB文档地址:https://www.mongodb.com/docs/manual/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值