系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Redis > 详细页面

MongoDB与SQL常用语法对应表

时间:2020-09-28来源:www.pcxitongcheng.com作者:电脑系统城

 对于经常写SQL的同学,一开始写mongoDB的语法会十分不习惯,下表整理了常见的SQL语法,函数以及相应的MongoDB对应语法,仅供参考。

 

术语及概念

SQL MongoDB
database database
table collection
row document
column field
index index
table joins $lookup
primary key primary key
SELECT INTO NEW_TABLE $out
MERGE INTO TABLE $merge(mongodb > 4.2)
UNION ALL $unionWith (mongodb > 4.4)
transactions transactions

 

语法对应表

SQL MongoDB

CREATE TABLE people (

    id MEDIUMINT NOT NULL

        AUTO_INCREMENT,

    user_id Varchar(30),

    age Number,

    status char(1),

    PRIMARY KEY (id)

)

db.createCollection("people")

 

db.people.insertOne( {

    user_id: "abc123",

    age: 55,

    status: "A"

 } )

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 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

db.people.find(

    { },

    { user_id: 1, status: 1, _id: 0 }

)

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 = 5

db.people.find(

    { $or: [ { status: "A" } , { age: 50 } ] }

)

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 AS

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.aggregate( [ { $group : { _id : "$status" } } ] )

or

db.people.distinct( "status" )

EXPLAIN SELECT *

FROM people

WHERE status = "A"

db.people.find( { status: "A" } ).explain()
UPDATE people

SET age = age + 3

WHERE status = "A"

db.people.updateMany(

   { status: "A" } ,

   { $inc: { age: 3 } }

)

分享到:

相关信息

  • redis实现session共享的方法

    引言大厂很多项目都是部署到多台服务器上,这些服务器在各个地区都存在,当我们访问服务时虽然执行的是同一个服务,但是可能是不同服务器运行的;在我学习项目时遇到这样一个登录情...

    2023-11-01

  • 简单聊一聊redis过期时间的问题

    1.多次修改一个redis的String过期键,如何保证他仍然能保留第一次设置时的删除时间 2.修改hash、set、Zset、list的值,会使过期时间重置吗?...

    2023-11-01

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载