MongoDB command and SQL syntax comparison

Source: Internet
Author: User

MongoDB vs. mysql command comparison

The traditional relational database is usually composed of three hierarchical concepts of database, table, record, and MongoDB is made up of database, collection (collection), Document object composed of three levels. MongoDB is for tables in relational databases, but there is no concept of columns, rows, and relationships in the collection, which embodies the nature of pattern freedom.

Mysql

Mongodb

Description

Mysqld

Mongod

Server daemon

Mysql

Mongo

Client Tools

Mysqldump

Mongodump

Logical Backup tool

Mysql

Mongorestore

Logical Recovery Tool

Db.repairdatabase ()

Repairing the database

Mysqldump

Mongoexport

Data Export Tool

Source

Mongoimport

Data Import Tool

Grant * privileges on *.

Db.adduser ()

Db.auth ()

New User and Permissions

Show databases

Show DBS

Show Library List

Show tables

Show Collections

Show Table List

Show slave status

Rs.status

Querying master-Slave status

Create table Users (a int, b int)

Db.createcollection ("Mycoll", {capped:true,

size:100000}) Another: The table can be created implicitly.

Create a table

Create INDEX idxname on users (name)

Db.users.ensureIndex ({name:1})

Create an index

Create INDEX idxname on users (Name,ts DESC)

Db.users.ensureIndex ({name:1,ts:-1})

Create an index

Insert into users values (1, 1)

Db.users.insert ({a:1, b:1})

Inserting records

Select A, B from users

Db.users.find ({},{a:1, b:1})

Query table

Select * from users

Db.users.find ()

Query table

Select * from users where age=33

Db.users.find ({age:33})

Conditional query

Select A, b from users where age=33

Db.users.find ({age:33},{a:1, b:1})

Conditional query

SELECT * from Users where age<33

Db.users.find ({' age ': {$lt: 33}})

Conditional query

SELECT * from users where age>33 and age<=40

Db.users.find ({' age ': {$gt:, $lte: 40}})

Conditional query

SELECT * from users where a=1 and b= ' Q '

Db.users.find ({a:1,b: ' Q '})

Conditional query

SELECT * from users where a=1 or b=2

Db.users.find ({$or: [{a:1}, {b:2}]})

Conditional query

SELECT * from users limit 1

Db.users.findOne ()

Conditional query

SELECT * from the users where name like "%joe%"

Db.users.find ({name:/joe/})

Fuzzy query

SELECT * from the users where name like "joe%"

Db.users.find ({name:/^joe/})

Fuzzy query

Select COUNT (1) from users

Db.users.count ()

Get table Record Count

Select COUNT (1) from users where age>30

Db.users.find ({age: {' $GT ': $}}). Count ()

Get table Record Count

Select DISTINCT last_name from Users

Db.users.distinct (' last_name ')

Remove duplicate values

SELECT * from the users ORDER by name

Db.users.find (). Sort ({name:-1})

Sort

SELECT * from Users ORDER by name DESC

Db.users.find (). Sort ({name:-1})

Sort

EXPLAIN SELECT * from Users where z=3

Db.users.find ({Z:3}). Explain ()

Get Storage Path

Update users set a=1 where b= ' Q '

Db.users.update ({b: ' Q '}, {$set: {a:1}}, False, True)

Update record

Update users set a=a+2 where b= ' Q '

Db.users.update ({b: ' Q '}, {$inc: {a:2}}, False, True)

Update record

Delete from users where z= "abc"

Db.users.remove ({z: ' abc '})

Deleting records

Db. Users.remove ()

Delete all the records

Drop database IF EXISTS test;

Use test

Db.dropdatabase ()

Deleting a database

drop table IF EXISTS test;

Db.mytable.drop ()

Delete Table/collection

Db.adduser (' Test ', ' test ')

Add user

Readonly-->false

Db.adduser (' Test ', ' test ', true)

Add user

Readonly-->true

Db.adduser ("Test", "test222")

Change Password

Db.system.users.remove ({User: "Test"})

or Db.removeuser (' test ')

Delete User

MongoDB command and SQL syntax comparison

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.