MySQL and MongoDB are common open source Databases , but MySQL is a traditional relational database, MongoDB is a non-relational database, also known as a document database, is a NoSQL database. They each have their own merits, the key is to see where to use. So the SQL (full name structured Query Language) statements that we know are not applicable to MongoDB, because SQL statements are the standard language for relational databases.
Take our company project for example, in the early project, all in the use of relational database, used SQLSERVER,ORACLE,DB2, and then all turned to MySQL, the reason is very simple: MySQL in the performance of a good situation, has the advantage of open source. MySQL's transactional and high-performance is our main consideration. Later, because the project to use the user system, there will be a large number of user data to interact-mass storage, MySQL read and write speed will have a little bottleneck, so we think of the recent development of a very strong nosql. In the early memcache of NoSQL, there are many non-relational databases, such as Redis,mongodb. After a period of testing, Redis and MongoDB read and write faster than MySQL has a clear advantage. The write speed of MongoDB is about 2.5w/times per second. MongoDB is stored in the Bson structure (binary), which has obvious advantages for mass data storage. Here is a comparison of the operation commands between MongoDB and MySQL.
Role |
Mysql |
Mongodb |
|
|
|
Server daemon |
Mysqld |
Mongod |
Client Tools |
Mysql |
Mongo |
Logical Backup tool |
Mysqldump |
Mongodump |
Logical Restore Tool |
Mysql |
Mongorestore |
Data Export Tool |
Mysqldump |
Mongoexport |
Data Import Tool |
Source |
Mongoimport |
|
|
|
Create a new user and authorize |
Grant All on * * to [email protected] ' localhost ' Identified by ' passwd '; |
Db.adduser ("User", "PSW") Db.auth ("User", "PSW") |
Show Library List |
show databases; |
Show DBS |
Go into the library |
Use dbname; |
Use dbname |
Show Table List |
Show tables; |
Show collections |
Querying master-Slave status |
show slave status; |
Rs.status |
Create a library |
Create database name; |
No need to create a separate, direct use in |
Create a table |
CREATE TABLE tname (id int); |
No need to create separately, insert data directly |
Delete a table |
drop table tname; |
Db.tname.drop () |
Delete a library |
Drop Database dbname; |
First go in the library, Db.dropdatabase () |
|
|
|
Inserting records |
INSERT into Tname (ID) value (2); |
Db.tname.insert ({id:2}) |
Deleting records |
Delete from tname where id=2; |
Db.tname.remove ({id:2}) |
Modify/Update records |
Update Tname Set id=3 where id=2; |
Db.tname.update ({id:2}, {$set: {id:3}},false,true) |
|
|
|
Querying All records |
SELECT * from Tname; |
Db.tname.find () |
Querying all Columns |
Select ID from Tname; |
Db.tname.find ({},{id:1}) |
Conditional query |
SELECT * from Tname where id=2; |
Db.tname.find ({id:2}) |
Conditional query |
SELECT * FROM Tname where ID < 2; |
Db.tname.find ({id:{$lt: 2}}) |
Conditional query |
SELECT * from Tname where ID >=2; |
Db.tname.find ({id:{$gte: 2}}) |
Conditional query |
SELECT * FROM Tname where id=2 and Name= ' Steve '; |
Db.tname.find ({id:2, Name: ' Steve '}) |
Conditional query |
SELECT * FROM Tname where id=2 Or name= ' Steve '; |
Db.tname.find ($or: [{id:2}, {name: ' Steve '}]) |
Conditional query |
SELECT * from Tname limit 1; |
Db.tname.findOne () |
|
|
|
Fuzzy query |
SELECT * from Tname where name Like "%ste%"; |
Db.tname.find ({name:/ste/}) |
Fuzzy query |
SELECT * from Tname where name Like "ste%"; |
Db.tname.find ({name:/^ste/}) |
|
|
|
Get table Record Count |
Select COUNT (id) from Tname; |
Db.tname.count () |
Get conditional The number of records |
Select COUNT (id) from Tname where id=2; |
Db.tname.find ({id:2}). Count () |
Remove when querying Duplicate value |
SELECT DISTINCT (last_name) From Tname; |
Db.tname.distinct (' last_name ') |
|
|
|
A query that is being sorted |
Select *from tname order by ID; |
Db.tname.find (). Sort ({id:1}) |
Inverse sort Query |
Select *from tname ORDER BY id Desc; |
Db.tname.find (). Sort ({id:-1}) |
|
|
|
Fetch storage Path |
Explain select * FROM Tname where id=3; |
Db.tname.find ({id=3}). Explain () |
Especially note: MongoDB Inserts multiple field syntax > Db.user.insert ({id:1,name: ' Steve ', Sex: ' Male '}) correctly > Db.user.insert ({id:2},{name: ' Bear '},{sex: ' Female '}) error
MySQL vs MongoDB operation contrast, and the difference