MongoDB command and SQL syntax comparison

Source: Internet
Author: User
Tags create index

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), Three levels of document objects. 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 syntax

MongoDB benefits Quite a lot, such as multi-column index, query can use some statistical functions, support multi-conditional query, but the current multi-table query is not supported, you can find a way to solve the problem of multi-table query through data redundancy.

MongoDB is very rich in data manipulation, here are some examples, the content is mostly from official documents, in addition to some of their own understanding.

Query Colls all data

Db.colls.find ()//select * from Colls

Querying by specifying criteria

Db.colls.find ({' last_name ': ' Smith '}),//select * from Colls where last_name= ' Smith '

Specify a multi-criteria query

Db.colls.find ({x:3, y: "foo"}),//select * from Colls where x=3 and y= ' foo '

Specify criteria range Query

Db.colls.find ({j: {$ne: 3}, K: {$gt: ten}});//select * from Colls where j!=3 and k>10

Query does not include a content

Db.colls.find ({}, {a:0});//query all data except for a of 0

Support <, <=, >= query, with symbolic substitution for $lt, $lte, $GT, $gte

Db.colls.find ({"field": {$gt: Value}});

Db.colls.find ({"field": {$lt: Value}});

Db.colls.find ({"field": {$gte: Value}});

Db.colls.find ({"field": {$lte: Value}});

You can also make a range query for a field

Db.colls.find ({"field": {$gt: value1, $lt: value2}});

Not equal to query character $ne

Db.colls.find ({x: {$ne: 3}});

In query with character $in

Db.colls.find ({"field": {$in: Array}});

Db.colls.find ({j:{$in: [2,4,6]}});

Not in query with character $nin

Db.colls.find ({j:{$nin: [2,4,6]}});

Character $mod for modulo query

Db.colls.find ({A: {$mod: [ten, 1]}})//where a% 10 = = 1

$all Query

Db.colls.find ({A: {$all: [2, 3]}});//Specifies that a satisfies any value in the array

$size Query

Db.colls.find ({A: {$size: 1});//The number of objects queried, this query queries the number of child objects of a 1 record

$exists Query

Db.colls.find ({A: {$exists: true}}); There is data for the A object

Db.colls.find ({A: {$exists: false}}); There is no data for the A object

$type Query the type value of the $type value to bsonhttp://bsonspec.org/data

Db.colls.find ({A: {$type: 2}}); Match A to string type data

Db.colls.find ({A: {$type: 16}}); Match A to int type data

Using regular expression matching

Db.colls.find ({name:/acme.*corp/i});//similar to SQL

Inline Object Query

Db.colls.find ({"Author.name": "Joe"});

1.3.3 versions and later versions include $not queries

Db.colls.find ({name: {$not:/acme.*corp/i}});

Db.colls.find ({A: {$not: {$mod: [10, 1]}}});

Sort () sorting

Db.colls.find (). Sort ({ts:-1});//1 to ascending 2 to descending

Limit () returns the number of restricted query data

Db.colls.find (). Limit (10)

Skip () skips some data

Db.colls.find (). Skip (10)

Snapshot () snapshot guarantees no duplicate data return or object loss

Count () counts the number of query objects

Db.students.find ({' address.state ': ' CA '}). Count ();//High efficiency

Db.students.find ({' address.state ': ' CA '}). ToArray (). length;//is inefficient

Group () resembles the group by function in SQL by grouping query results

Distinct () returns non-repeating values

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.