Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the comparison and difference between MySQL and MongoDB operation commands?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you the comparison and difference of operation commands between MySQL and MongoDB. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

MySQL and MongoDB are commonly used open source databases, but MySQL is a traditional relational database, MongoDB is a non-relational database, also known as document database, is a NoSQL database. Each of them has its own advantages, the key is to see where to use it. So the SQL (full name Structured Query Language) statements we are familiar with do not apply to MongoDB, because SQL statements are the standard language for relational databases.

Take our company's projects as an example, in the early projects, they all used relational databases, used SQLServer,Oracle,DB2, and then all turned to Mysql for a simple reason: Mysql has the advantage of open source when its performance is good. The transactionality and high performance of Mysql are our main considerations. Later, because the project uses a user system, that is, there will be a large amount of user data to interact with each other-mass storage, the reading and writing speed of Mysql will be a little bottleneck, so we think of the recent strong development of Nosql. With the development of memcache in the early days of Nosql, there are many non-relational databases, such as redis,mongodb. After a period of testing, redis and mongodb do have obvious advantages over Mysql in reading and writing speed. The write speed of mongodb is about 2.5W/ times per second. Mongodb uses BSON structure (binary) for storage, which has obvious advantages for mass data storage.

The following is a comparison of the operation commands of Mongodb and Mysql.

Server daemon mysqld/mongod

Client tool 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 username@'localhost' identified by 'passwd';db.addUser ("user", "psw") db.auth ("user", "psw")

Show library list show databases;show dbs

Go to the library use dbname;use dbname

Show table list show tables;show collections

Query master-slave status show slave status;rs.status

You don't need to create the library create database name; separately, just use it.

Create table create table tname (id int); insert data directly without creating it separately

Delete table drop table tname;db.tname.drop ()

Delete the library drop database dbname; first go to the library, db.dropDatabase ()

Insert record insert into tname (id) value (2); db.tname.insert ({id:2})

Delete record delete from tname where id=2;db.tname.remove ({id:2})

Modify / update record update tname set id=3 where id=2;db.tname.update ({id:2}, {$set: {id:3}}, false,true)

Query all records select * from tname;db.tname.find ()

Query 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}});   条件查询select * from tname where id >

= 2terdb.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 number of table records select count (id) from tname;db.tname.count ()

Get the number of conditional records select count (id) from tnamewhere id=2;db.tname.find ({id:2}) .count ()

Remove the duplicate value select distinct (last_name) from tname;db.tname.distinct ('last_name') when querying

Positive sort query select * from tname order by id;db.tname.find () .sort ({id:1})

Reverse sort query select * from tname order by id desc;db.tname.find () .sort ({id:-1})

Take the storage path explain select * from tname where id=3;db.tname.find ({id=3}) .explain ()

It is important to note that mongodb inserts multiple field syntax.

> db.user.insert ({id:1,name:'steve',sex:'male'}) is correct.

> db.user.insert ({id:2}, {name:'bear'}, {sex:'female'}) error.

The above is the comparison and difference between MySQL and MongoDB operation commands. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report