Comparison between basic SQL statements of MongoDB and MySQL Databases

Source: Internet
Author: User
Query: MySQL: SELECT * FROMuserMongo: db. user. find () MySQL: SELECT * FROMuserWHEREname 'starlil' Mongo: db. user. find ({'name': 'starlil'}) insert: MySQL: INSERTINOTuser ('name', 'age') va

Query:

MySQL:
SELECT * FROM user
Mongo:
Db. user. find ()

MySQL:
SELECT * FROM user WHERE name = 'starlil'
Mongo:
Db. user. find ({'name': 'starlil '})

Insert:
MySQL:
Insert inot user ('name', 'age') values ('starlil', 25)
Mongo:
Db. user. insert ({'name': 'starlee ', 'age': 25 })

To add a field to MySQL, you must:
Alter table user ....
However, in MongoDB, you only need:
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 :<=; $ ne :! =

Update:

MySQL:
UPDATE user SET 'age' = 36 WHERE 'name' = 'starlil'
Mongo:
Db. user. update ({'name': 'starlil'}, {$ set: {'age': 36 }})

MySQL:
UPDATE user SET 'age' = 'age' + 3 WHERE 'name' = 'starlil'
Mongo:
Db. user. update ({'name': 'starlil'}, {$ inc: {'age': 3 }})

MySQL:
Select count (*) FROM user WHERE 'name' = 'starlil'
Mongo:
Db. user. find ({'name': 'starlil'}). count ()

MySQL:
SELECT * FROM user limit 10, 20
Mongo:
Db. user. find (). skip (10). limit (20)

MySQL:
SELECT * FROM user WHERE 'age' IN (25, 35, 45)
Mongo:
Db. user. find ({'age': {$ in: [25, 35, 45]})

MySQL:
SELECT * FROM user order by age DESC
Mongo:
Db. user. find (). sort ({'age':-1 })

MySQL:
Select distinct (name) FROM user WHERE age> 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 })

# Yesterday I madeMysql(Jdbc) andMongodb(Java driver) Insert 10 0000 data records for comparison

Mysql inserts one insert10 at a time for 1817487 ms

Mysql inserts and executes One insert operation in batches for 2358 ms

Mongodb inserts one insert10 at a time for 5489 ms

Mongodb is regarded as insert10 for ten thousand times on the surface, but it may only take one to three times to write data to the database, which is similar to the second mysql method.

From this perspective, mongodb insertion should be faster than mysql under the same circumstances.

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.