MongoDB corresponding SQL statement

Source: Internet
Author: User
Tags create index createindex

-------------------mongodb Corresponding SQL statement-------------------1, Create and alter    1,     sql:        CREATE TABLE users (            ID mediuminty not NULL auto_increment,  &N Bsp         user_id varchar (+),            Age number,            Status char (1),            PRIMARY key (ID)         )     mongodb:        Db.users.insert ({            user_id: "Xia Ohao ",            age:55,            Status:" A "    & nbsp  })      2,     sql:        ALTER TABLE users add Join_date datetime&nbsp ;   mongodb:        db.users.update (            {},    &NBS P   &NBSP;   {$set: {join_date:new date ()}},            {multi:true}       )     3,     sql:        ALTER TABLE users drop column join_date    MongoDB :        db.users update (            {},        &NBS P   {$unset: {join_date: ""}},            {multi:true}       )   & nbsp 4,     sql:        CREATE index IDX_USER_ID_ASC on user (user_id)     mongodb:        Db.users.createIndex ({user_id:1})     5,     sql:        creat E index idx_user_id_desc on user (User_id,age desc)     mongodb:        Db.users.createIndex ( {user_id:1,age:-1})     6,     sql:        DROP table users    mongodb:    &NBSP   Db.users.drop ()  2, insert    1,     sql:        Insert into users (user_ Id,age,status) VALUES ("001", 18,true)     mongodb:        Db.users.insert (    & nbsp       {user_id: "001",age:18,status:true}       )  3, select    1,     sql:        SELECT * from users    mongodb:        Db.user S.find ()     2,     sql:        Select Id,user_id,status from users    Mon godb:        Db.users.find ({"user_id": 1, "Status": 1})     3,     sql:        Select User_id,status from users    mongodb:        Db.users.find (  &nbsp ;         {},            {"user_id": 1, "status": 1, "_id":0}    &NB Sp  )     4,    sql:        SELECT * from users where status= "A"     mongodb:    &NBS P   Db.users.find (            {"Status": "A"}       )     5,     sql:        Select User_id,status from users where status= "A"     MONGODB:&NBSP ;       Db.users.find (            {"Status": "A"},        &N Bsp   {"user_id": 1, "status": 1, "_id":0}       )     6,     sql:    &NBSP ;   SELECT * from users where status!= "A"     mongodb:        Db.users.find (            {"status": {$ne: "A"}}       )     7,     sql:  &nbs P     SELECT * from users where status= "A" and age=18    mongodb:        db.users.fi nd (           {                "status": "A",    &NB Sp           "age":18           }       )     8,     sql:        SELECT * from users where status= "A" or age= "    MONGO db:        Db.users.find (            {$or: [{' Status ': ' A '},                    {"Age": 9}]}       )    ,     sql:        SELECT * from users where age>10    mongodb:      &NBS P Db.users.find (            {"Age": {$gt:10}}       )     10, &nbs P   sql:        SELECT * from users where age<25    mongodb:      &NBSP ; Db.users.find(            {"Age": {$lt:25}}       )     11,     SQL :        SELECT * from users where age<25 and age>=8    mongodb:      & nbsp Db.users.find (            {"Age": {$gte: 8, $lt:25}}       )      12,     sql:        SELECT * from the users where user_name like "%gh%"     MONGO db:        Db.users.find (            {"user_name":/gh/}    &NB Sp  )     13,     sql:        SELECT * from the users where user_name like "wang%" &nb Sp   mongodb:        Db.users.find (            {"user_name":/^wang/}& nbsp      )     14,     sql:        SELECT * from users where status= "A" Orderby user_id asc    mongodb:        Db.users.find (            {") Status ":" A "}       ). Sort (            {" user_id ":1}       )     15,     sql:        SELECT * from users where status= ' A ' ORDER by user _id desc    mongodb:        Db.users.find (            {"status") ":" A "}          ). Sort (            {" user_id ":-1}  &nbsp ;         16,     sql:        SELECT COUNT (*) from users    Mong odb:        Db.users.find (). Count ()     17,     sql:        Sele CT count (user_id) from users    mongodb:        Db.users.find (        &N Bsp   {"user_id": {$exists:true}}       ). Count ()     18,     sql:        SELECT Count (*) from users where age>20    mongodb:        Db.users.find (      & nbsp     {"Age": {$gt:20}}       ). Count ()     19,     sql:        SELECT DISTINCT (status) from users    mongodb:        db.users.distinct ("user_id") # To regain the corresponding data information     20,     sql:        SELECT * from users limit 1    MongoD b:        db.users.find (). Limit (1)     21,     sql:        Selec T * from users limit 5 skip 10    mongodb:        db.users.find (). Limit (5). Skip (Ten)   &N Bsp 22,     sql:        Explain select * from users where status= "A"     mongodb:       Db.users.find (            {"Status": "A"}       ). Explain () &nbsp ; 4, delete records    1,     sql:        Delete from users where status = "D"     mongodb:        Db.users.remove (            {"Status": "D"}       )     2,     sql:        DELETE from users    MongoD b:        Db.users.remove ({})

MongoDB corresponding SQL statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.