Comparison of MongoDB commands and SQL syntax

Source: Internet
Author: User
Tags mongodb commands mysql commands

Comparison between MongoDB and MySQL commands

Traditional relational databases are generally composed of three levels: database, table, and record. MongoDB is composedDatabase, collection, and document)Three layers. MongoDB has no concept of columns, rows, and links for tables in relational databases, which reflects the free mode.

 

MySQL

MongoDB

Description

Mysqld

Mongod

Server daemon

MySQL

Mongo

Client tools

Mysqldump

Mongodump

Logical backup tools

MySQL

Mongorestore

Logical recovery tool

 

DB. repairdatabase ()

Restore database

Mysqldump

Mongoexport

Data export tool

Source

Export Import

Data Import Tool

Grant * privileges on *. *...

DB. adduser ()

DB. Auth ()

Create a user and grant permissions

Show Databases

Show DBS

Show library list

Show tables

Show collections

Display table list

Show slave status

Rs. Status

Query Master/Slave status

Create Table users (A int, B INT)

DB. createcollection ("mycoll", {capped: True,

Size: 100000}) and implicitly create a table.

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 })

Insert record

Select a, B from users

DB. Users. Find ({}, {A: 1, B: 1 })

Query a table

Select * from users

DB. Users. Find ()

Query a 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: 33, $ 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 users where name like "% Joe %"

DB. Users. Find ({Name:/Joe /})

Fuzzy search

Select * from users where name like "Joe %"

DB. Users. Find ({Name:/^ Joe /})

Fuzzy search

Select count (1) from users

DB. Users. Count ()

Obtain the number of table records

Select count (1) from users where age> 30

DB. Users. Find ({age: {'$ GT': 30}). Count ()

Obtain the number of table records

Select distinct last_name from users

DB. Users. Distinct ('Last _ name ')

Remove duplicate values

Select * from 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 ()

Obtain the 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 '})

Delete record

 

DB. Users. Remove ()

Delete all records

Drop database if exists test;

Use test

DB. dropdatabase ()

Delete 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 a user

 

Use Admin

Superuser

 

DB. Auth ('test', 'test ')

User authorization

 

DB. system. Users. Find ()

View User List

 

Show users

View All Users

 

DB. printcollectionstats ()

View the status of each collection

 

DB. printreplicationinfo ()

View master-slave replication status

 

Show Profile

View profiling

 

DB. copydatabase ('mail _ ADDR ', 'mail _ addr_tmp ')

Copy Database

 

DB. Users. datasize ()

View Collection data size

 

DB. Users. totalindexsize ()

Query index size

 

 

 MongoDB syntax

 

MongoDB has many advantages, such as multi-column indexes. Some statistical functions can be used for queries and multi-condition queries are supported. However, multi-table queries are not currently supported, you can try to solve the problem of multi-Table query through data redundancy.

MongoDB provides a wealth of data operations. The following are some examples. Most of the content is from the official documentation, and others are for your understanding.

 

Query all colls data

DB. colls. Find () // select * From colls

 

Query by specified conditions

DB. colls. Find ({'last _ name': 'Smith '}); // select * From colls where last_name = 'Smith'

 

Specify multi-condition Query

DB. colls. Find ({X: 3, Y: "Foo"}); // select * From colls where x = 3 and Y = 'foo'

 

Query by specified condition range

DB. colls. Find ({J: {$ ne: 3}, K: {$ GT: 10}); // select * From colls where J! = 3 and K> 10

 

The query does not include any content.

DB. colls. Find ({}, {A: 0}); // query all data except that A is 0.

 

Supported <, <=,>,> =, and must be replaced by $ lt, $ LTE, $ GT, and $ 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 query the range of a field.

DB. colls. Find ({"field": {$ GT: value1, $ LT: value2 }});

 

Not equal to $ ne

DB. colls. Find ({x :{$ ne: 3 }});

 

Character $ in for in Query

DB. colls. Find ({"field": {$ in: array }});

DB. colls. Find ({J: {$ in: [2, 4, 6]});

 

Not in query character $ Nin

DB. colls. Find ({J: {$ Nin: [2, 4, 6]});

 

MoD query character $ mod

DB. colls. Find ({A: {$ mod: [10, 1]}) // Where a % 10 = 1

 

$ All Query

DB. colls. Find ({A :{$ All: [2, 3] }}); // when specifying a to satisfy any value in the array

 

$ Size Query

DB. colls. Find ({A: {$ size: 1}); // queries the number of objects. This query queries records with the number of sub-objects of a being 1.

 

$ Exists Query

DB. colls. Find ({A: {$ exists: true}); // data of object

DB. colls. Find ({A :{$ exists: false }}); // The data of object A does not exist.

 

$ Type query $ type value: type value of bsonhttp: // bsonspec.org/data

DB. colls. Find ({A: {$ type: 2}); // match data of the string type as

DB. colls. Find ({A: {$ type: 16}); // match a as int type data

 

Match with regular expressions

DB. colls. Find ({Name:/acme. * Corp/I}); // similar to like

 

Embedded object query

DB. colls. Find ({"author. Name": "Joe "});

 

Version 1.3.3 and later include $ not query

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 is in ascending order. 2 is in descending order.

 

Limit () Limit the number of returned data queries

DB. colls. Find (). Limit (10)

 

Skip () skips some data

DB. colls. Find (). Skip (10)

 

Snapshot () snapshot ensures that no duplicate data is returned or objects are lost

 

Count () count the number of queried objects

DB. Students. Find ({'address. state': 'CA'}). Count (); // High Efficiency

DB. Students. Find ({'address. state': 'CA'}). toarray (). length; // very inefficient

 

The group () function for query result grouping is similar to the group by function in SQL.

Distinct () returns a non-repeated Value

 

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.