📜  SQL到MongoDB的映射

📅  最后修改于: 2020-11-23 01:19:54             🧑  作者: Mango

SQL到MongoDB的映射

下表列出了各种SQL术语和概念,与MongoDB术语和概念相似。

SQL Terms MongoDB Terms
database Database
table Collection
row document or BSON document
column field
index index
table joins $lookup, embedded document
primary key primary key
In SQL, we can specify any unique column or column combination as the primary key. In MongoDB, we don’t need to set the primary key. The _id field is automatically set to the primary key.
aggregation aggregation pipeline
SELECT INTO NEW_TABLE $out
MERGE INTO TABLE $merge
transactions transactions

下面的示例表示各种SQL语句和类似的MongoDB语句。

表格中的示例假定以下条件:

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

创建和更改命令

SQL statements MongoDB statements
CREATE TABLE JavaTpoint (
    id MEDIUMINT NOT NULL
        AUTO_INCREMENT,
    user_id Varchar(20),
    age Number,
    status char(1),
    PRIMARY KEY (id)
)
db.createCollection ( " JavaTpoint " )
ALTER TABLE JavaTpoint ADD join_date DATETIME
db.JavaTpoint.updateMany(
    { },
    { $set: { join_date: new Date() } }
)
ALTER TABLE JavaTpoint DROP COLUMN join_date
db.JavaTpoint.updateMany(
    { },
    { $unset: { "join_date": "" } }
)
CREATE INDEX idx_user_id_asc ON JavaTpoint ( user_id )
db.people.createIndex ( { user_id: 1 } )
CREATE INDEX idx_user_id_asc ON people (user_id)
db.people.createIndex( { user_id: 123, age: 1} )
DROP TABLE people
db.people.drop ()

MongoDB和SQL插入语句

SQL Insert statement MongoDB insert statement
INSERT INTO JavaTpoint (user_id,
                  age,
                  status)
VALUES ("mongo",
        45,
        "A")
db.JavaTpoint.insertOne(
   { user_id: "mongo", age: 18, status: "A" }
)

SQL和Mongo DB选择命令

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

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

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

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

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

db. JavaTPoint.find( { age: { $gt: 30 } } ).count()
SELECT DISTINCT(status)
FROM JavaTPoint
db. JavaTPoint.aggregate( [ { $group : { _id : "$status" } } ] )
or, for distinct value sets that do not exceed the BSON size limit

db. JavaTPoint.distinct( "status" )
SELECT *
FROM JavaTPoint
LIMIT 1
db. JavaTPoint.findOne()
or

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

SQL和MongoDB更新语句

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

SQL和MongoDB删除语句

SQL Delete Statements MongoDB deleteMany() Statements
DELETE FROM JavaTpoint WHERE status = "D"
db.JavaTpoint.deleteMany( { status: "D" } )
DELETE FROM JavaTpoint
db.JavaTpoint.deleteMany( { } )