当前位置:网站首页>Common syntax corresponding table of mongodb and SQL

Common syntax corresponding table of mongodb and SQL

2020-11-06 21:24:00 Irving the procedural ape

    For writing often SQL Classmate , Write at the beginning mongoDB I'm not used to it , The following table sorts out the common SQL grammar , Function and corresponding MongoDB Correspondence grammar , For reference only .

 

Terms and concepts

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

 

Grammar correspondence table

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(

    { },

&n.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢