SQL到MongoDB的映射

SQL到MongoDB的映射

下表介绍了各种SQL术语和概念,这些术语和概念与MongoDB的术语和概念类似。

SQL术语 MongoDB术语
数据库 数据库
集合
文档或BSON文档
字段
索引 索引
表连接 $lookup,嵌入式文档
主键 主键
在SQL中,我们可以将任何唯一的列或列组合指定为主键。 在MongoDB中,我们不需要设置主键。_id字段会自动设置为主键。
聚合 聚合管道
SELECT INTO NEW_TABLE $out
MERGE INTO TABLE $merge
事务 事务

以下示例代表了各种SQL语句和类似的MongoDB语句。

表中的示例假设以下条件:

  • SQL示例假设表名为JavaTpoint
  • MongoDB示例假设集合名为JavaTpoint,并包含以下原型的文档:
{
  _id: ObjectId("509a8fb2bd2f983a0"),
  user_id: "admin123",
  age: 18,
  status: 'A'
}

创建和修改命令

SQL语句

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

MongoDB语句

db.createCollection ( " JavaTpoint " )

SQL语句

ALTER TABLE JavaTpoint ADD join_date DATETIME

MongoDB语句

db.JavaTpoint.updateMany(
    { },
    { $set: { join_date: new Date() } }
)

SQL语句

ALTER TABLE JavaTpoint DROP COLUMN join_date

MongoDB语句

db.JavaTpoint.updateMany(
    { },
    { $unset: { "join_date": "" } }
)

SQL语句

CREATE INDEX idx_user_id_asc ON JavaTpoint ( user_id )

MongoDB语句

db.people.createIndex ( { user_id: 1 } )
CREATE INDEX idx_user_id_asc ON people (user_id)

SQL语句

db.people.createIndex( { user_id: 123, age: 1} )
DROP TABLE people

MongoDB语句

db.people.drop ()

MongoDB和SQL插入语句

SQL语句

INSERT INTO JavaTpoint (user_id,
                  age,
                  status)
VALUES ("mongo",
        45,
"A")

MongoDB语句

db.JavaTpoint.insertOne(
   { user_id: "mongo", age: 18, status: "A" }
)

SQL 和Mongo DB 选择命令

SQL语句

SELECT *
FROM JavaTpoint

MongoDB语句

db.JavaTpoint.find()

SQL语句

SELECT id, user_id, status FROM JavaTpoint

MongoDB语句

db.JavaTpoint.find( { }, { user_id: 1, status: 1 } )

SQL语句

SELECT user_id, status FROM JavaTpoint

MongoDB语句

db.JavaTpoint.find( { }, { user_id: 1, status: 1, _id: 0 } )

SQL语句

SELECT * FROM JavaTpoint WHERE status = "B"

MongoDB语句

db.JavaTpoint.find( { status: "A" } )

SQL语句

SELECT user_id, status FROM JavaTpoint WHERE status = "A"

MongoDB语句

db.javaTpoint.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 } )

SQL语句

SELECT * FROM JavaTpoint WHERE status != "A"

MongoDB语句

db.JavaTpoint.find( { status: { $ne: "A" } } )

SQL语句

SELECT *
FROM JavaTpoint
WHERE status = "A"
AND age = 50

MongoDB语句

db.JavaTpoint.find(
    { status: "A",
      age: 50 }
)

SQL语句

SELECT *
FROM JavaTpoint
WHERE status = "A"
OR age = 50

MongoDB语句

db.JavaTpoint.find(
    { $or: [ { status: "A" } , { age: 50 } ] }
)

SQL语句

SELECT *
FROM JavaTpoint
WHERE age > 25

MongoDB语句

db.JavaTpoint.find(
    { age: { $gt: 25 } }
)

SQL语句

SELECT *
FROM JavaTpoint
WHERE age < 25

MongoDB语句

Db.JavaTpoint.find(
   { age: { $lt: 25 } }
)

SQL语句

SELECT *
FROM JavaTpoint
WHERE age > 25
AND   age <= 50

MongoDB语句

db.JavaTpoint.find(
   { age: { gt: 25,lte: 50 } }
)

SQL语句

SELECT *
FROM JavaTpoint
WHERE user_id like "%bc%"

MongoDB语句

db.JavaTpoint.find( { user_id: /bc/ } )
或者

db.JavaTpoint.find( { user_id: { $regex: /bc/ } } )

SQL语句

SELECT *
FROM JavaTpoint
WHERE user_id like "bc%"

MongoDB语句

db.JavaTpoint.find( { user_id: /^bc/ } )
或者

db.JavaTpoint.find( { user_id: { $regex: /^bc/ } } )

SQL语句

SELECT *
FROM JavaTPoint
WHERE status = "A"
ORDER BY user_id ASC

MongoDB语句

db. JavaTPoint. find( { status: "A" } ). sort( { user_id: 1 } )

SQL语句

SELECT *
FROM JavaTPoint
WHERE status = "A"
ORDER BY user_id ASC

MongoDB语句

db. JavaTPoint. find( { status: "A" } ). sort( { user_id: 1 } )

SQL语句

SELECT *
FROM JavaTPoint
WHERE status = "A"
ORDER BY user_id ASC

MongoDB语句

db. JavaTPoint. find( { status: "A" } ). sort( { user_id: 1 } )

SQL语句

SELECT *
FROM JavaTPoint
WHERE status = "A"
ORDER BY user_id DESC

MongoDB语句

db. JavaTPoint. find( { status: "A" } ). sort( { user_id: -1 } )

SQL语句

SELECT *
FROM JavaTPoint
WHERE status = "A"
ORDER BY user_id DESC

MongoDB语句

db.JavaTPoint.find({ status: "A" }).sort({ user_id: -1 })

SQL语句

SELECT COUNT(*)
FROM JavaTPoint

MongoDB语句

db.JavaTPoint.count()
或

db.JavaTPoint.find().count()

SQL语句

SELECT COUNT(user_id)
FROM JavaTPoint

MongoDB语句

db.JavaTPoint.count({ user_id: { exists: true } })
或

db.JavaTPoint.find({ user_id: {exists: true } }).count()

SQL语句

SELECT COUNT(*)
FROM JavaTPoint
WHERE age > 30

MongoDB语句

db.JavaTPoint.count({ age: { gt: 30 } })
或

db.JavaTPoint.find({ age: {gt: 30 } }).count()

SQL语句

SELECT DISTINCT(status)
FROM JavaTPoint

MongoDB语句

db. JavaTPoint.aggregate( [ { group : { _id : "status" } } ] )
或者,对于不超过BSON大小限制的不同值集合

db. JavaTPoint.distinct( "status" )

SQL语句

SELECT *
FROM JavaTPoint
LIMIT 1

MongoDB语句

db. JavaTPoint.findOne()
或者

db. JavaTPoint.find(). limit(1)

SQL语句

SELECT *
FROM JavaTPoint
LIMIT 5
SKIP 10

MongoDB语句

db. JavaTPoint.find(). limit(5). skip(10)

SQL语句

EXPLAIN SELECT *
FROM JavaTPoint WHERE status = "A"

MongoDB语句

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

SQL和MongoDB更新语句

SQL语句

UPDATE JavaTpoint SET status = "C"
WHERE age > 25

MongoDB语句

db.JavaTpoint.updateMany( { age: { gt: 25 } }, {set: { status: "C" } } )

SQL语句

UPDATE JavaTpoint SET age = age + 3
WHERE status = "A"

MongoDB语句

db.JavaTpoint.updateMany( { status: "A" } , { $inc: { age: 3 } } )

SQL和MongoDB删除语句

SQL语句

DELETE FROM JavaTpoint WHERE status = "D"

MongoDB语句

db.JavaTpoint.deleteMany( { status: "D" } )

SQL语句

DELETE FROM JavaTpoint

MongoDB语句

db.JavaTpoint.deleteMany( { } )

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程