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's the difference between MySQL and MongoDB?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what is the difference between MySQL and MongoDB. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Query:

MySQL:

SELECT * FROM user

Mongo:

Db.user.find ()

MySQL:

SELECT * FROM user WHERE name = 'starlee'

Mongo:

Db.user.find ({'name':' starlee'})

Insert:

MySQL:

INSERT INOT user (`name`, `age`) values ('starlee',25)

Mongo:

Db.user.insert ({'name':' starlee', 'age': 25})

If you want to add a field to MySQL, you must:

ALTER TABLE user... .

But in MongoDB, all you need is:

Db.user.insert ({'name':' starlee', 'age': 25,' email': 'starlee@starlee.com'})

Delete:

MySQL:

DELETE * FROM user

Mongo:

Db.user.remove ({})

MySQL:

DELETE FROM user WHERE age

< 30 Mongo: db.user.remove({'age' : {$lt : 30}}) $gt : >

; $gte: > =; $lt:

< ; $lte : 20 Mongo: db.user.distinct('name', {'age': {$lt : 20}}) MySQL: SELECT name, sum(marks) FROM user GROUP BY name Mongo: db.user.group({ key : {'name' : true}, cond: {'name' : 'foo'}, reduce: function(obj,prev) { prev.msum += obj.marks; }, initial: {msum : 0} }); MySQL: SELECT name FROM user WHERE age < 20 Mongo: db.user.find('this.age < 20′, {name : 1}) 发现很多人在搜MongoDB循环插入数据,下面把MongoDB循环插入数据的方法添加在下面: for(var i=0;i33 db.users.find({'age':{$gt:33}})}) SELECT * FROM users WHERE age33 AND age 30 db.users.find({age: {'$gt': 30}}).count() SELECT COUNT(AGE) from users db.users.find({age: {'$exists': true}}).count() UPDATE users SET a=1 WHERE b='q' db.users.update({b:'q'}, {$set:{a:1}}, false, true) UPDATE users SET a=a+2 WHERE b='q' db.users.update({b:'q'}, {$inc:{a:2}}, false, true) DELETE FROM users WHERE z="abc" db.users.remove({z:'abc'}); ################################################### 一、操作符 操作符相信大家肯定都知道了,就是等于、大于、小于、不等于、大于等于、小于等于,但是在mongodb里不能直接使用这些操作符。在mongodb里的操作符是这样表示的: (1) $gt >

(greater than)

(2) $lt

< (小于)    (3) $gte  >

= (greater than or equal to)

(4) $lt insert ({'name' = >' caleng', 'email' = >' admin#admin.com'})

Isn't it always simple? yes, it's that simple. It has no field restrictions. You can name it at will and insert data.

The copy code is as follows:

Db.collection.update ({"count": {$gt: 1}}, {$set: {"test2": "OK"}}); only the first record greater than 1 has been updated

Db.collection.update ({"count": {$gt: 3}}, {$set: {"test2": "OK"}}, false,true); all records greater than 3 are updated

Db.collection.update ({"count": {$gt: 4}}, {$set: {"test5": "OK"}}, true,false); only the first entry is added to records greater than 4

Db.collection.update ({"count": {$gt: 5}}, {$set: {"test5": "OK"}}, true,true); all records greater than 5 are added

Query

The copy code is as follows:

Db.collection.find (array ('name' = >' bailing'), array ('email'= >' email@qq.com'))

Db.collection.findOne (array ('name' = >' bailing'), array ('email''email@qq.com'))

You can see that I wrote the query in two different ways, which is why, in fact, it is the same as cooking, with different seasonings, the fried dishes have different tastes. Let me tell you about the different effects of these two seasonings.

FindOne () returns only one document object, and find () returns a list of collections.

That is to say, if we only want to check the details of a particular piece of data, we can use findOne ()

If you want to query a certain set of information, such as a news list, we can use find ()

Well, I think everyone will think at this time that I want to sort this list. No problem mongodb will serve you wholeheartedly.

The copy code is as follows:

Db.collection.find () .sort ({age:1}); / / in the positive order of age

Db.collection.find () .sort ({age:-1}); / / in reverse order of age

Db.collection.count (); / / get the total data

Db.collection.limit (1); / / start position of fetching data

Db.collection.skip (10); / / the end position of fetching data

In this way, we have implemented an operation that takes 10 pieces of data and sorts them.

Delete

There are two operations to delete: remove () and drop ()

The copy code is as follows:

Db.collection.remove ({"name", 'jerry'}) / / Delete specific data

Db.collection.drop () / / Delete all data in the collection

Distinct operation

The copy code is as follows:

Db.user.distinct ('name', {' age': {$lt: 20}})

two。 Familiar with MongoDB data manipulation statements, sql-like

Database operation syntax

Mongo-path

Db.AddUser (username,password) add user

Db.auth (usrename,password) sets up database connection verification

Db.cloneDataBase (fromhost) Clones a database from the target server

Db.commandHelp (name) returns the help for the command

Db.copyDatabase (fromdb,todb,fromhost) replication database fromdb--- source database name, todb--- target database name, fromhost--- source database server address

Db.createCollection (name, {size:3333,capped:333,max:88888}) creates a dataset, which is equivalent to a table

Db.currentOp () cancels the current operation of the current library

Db.dropDataBase () deletes the current database

Db.eval (func,args) run code server-side

Db.getCollection (cname) gets a collection of data. Synonym: db ['cname'] or db.cname

Db.getCollenctionNames () gets a list of the names of all data sets

Db.getLastError () returns the last error message

Db.getLastErrorObj () returns the last wrong object

Db.getMongo () gets the connection object get the server connection object of the current server

Db.getMondo (). SetSlaveOk () allow this connection to read from then nonmaster membr of a replica pair

Db.getName () returns the name of the database to be operated on

Db.getPrevError () returns the last error object

Db.getProfilingLevel ()? What level?

Db.getReplicationInfo ()? What information?

Db.getSisterDB (name) get the db at the same server as this onew

Db.killOp () stops (kills) the current operation in the current library

Db.printCollectionStats () returns the dataset status of the current library

Db.printReplicationInfo ()

Db.printSlaveReplicationInfo ()

Db.printShardingStatus () returns whether the current database is a shared database

Db.removeUser (username) Delete user

Db.repairDatabase () repairs the current database

Db.resetError ()

Db.runCommand (cmdObj) run a database command. If cmdObj is a string, turns it into {cmdObj:1}

Db.setProfilingLevel (level) 0 hours offline 1 month 2 months all

Db.shutdownServer () closes the current service program

Db.version () returns the version information of the current program

Dataset (table) operation syntax

Db.linlin.find ({id:10}) returns the dataset of the linlin dataset ID=10

Db.linlin.find ({id:10}) .count () returns the total number of data from the linlin dataset ID=10

Db.linlin.find ({id:10}) .limit (2) returns the dataset of the linlin dataset ID=10 the dataset starting from Article 2

Db.linlin.find ({id:10}) .skip (8) returns the dataset of the linlin dataset ID=10 from 0 to 8

Db.linlin.find ({id:10}) .limit (2) .skip (8) returns data from articles 2 to 8 of the dataset of the linlin dataset ID=1=

Db.linlin.find ({id:10}) .sort () returns the sorted dataset of the linlin dataset ID=10

Db.linlin.findOne ([query]) returns a piece of data that meets the criteria

Db.linlin.getDB () returns the name of the database to which this dataset belongs

Db.linlin.getIndexes () returns the index information of some datasets

Db.linlin.group ({key:...,initial:...,reduce:... [, cond:...]})

Db.linlin.mapReduce (mayFunction,reduceFunction,)

Db.linlin.remove (query) deletes a piece of data in the dataset

Db.linlin.renameCollection (newName) renames some dataset names

Db.linlin.save (obj) inserts a piece of data into the dataset

Db.linlin.stats () returns the status of this dataset

Db.linlin.storageSize () returns the storage size of this dataset

Db.linlin.totalIndexSize () returns the index file size for this dataset

Db.linlin.totalSize () returns the total size of some datasets

Db.linlin.update (query,object [, upsert_bool]) updates a piece of data in this dataset

Db.linlin.validate () validates this dataset

Db.linlin.getShardVersion () returns the dataset shared version number

This is the end of the article on "what's the difference between MySQL and MongoDB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report