Detailed description of simple comparison table from MySQL to MongoDB

Source: Internet
Author: User
Tags comparison table
This article provides a detailed analysis of the simple comparison table from MySQL to MongoDB. For more information, see MongoDB.

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 })
Many people found that they are searching for MongoDB to insert data cyclically. The following describes how to insert data cyclically in MongoDB:
For (var I = 0; I <100; I ++) db. test. insert ({uid: I, uname: 'nosqlfan '+ I });
Insert one hundred data records at a time. the approximate structure is as follows:
{"_ Id": ObjectId ("4c876e519e86023a30dde6b8"), "uid": 55, "uname": "nosqlfan55 ″}
{"_ Id": ObjectId ("4c876e519e86023a30dde6b9"), "uid": 56, "uname": "nosqlfan56 ″}
{"_ Id": ObjectId ("4c876e519e86023a30dde6ba"), "uid": 57, "uname": "nosqlfan57 ″}
{"_ Id": ObjectId ("4c876e519e86023a30dde6bb"), "uid": 58, "uname": "nosqlfan58 ″}
{"_ Id": ObjectId ("4c876e519e86023a30dde6bc"), "uid": 59, "uname": "nosqlfan59 ″}
{"_ Id": ObjectId ("4c876e519e86023a30dde6bd"), "uid": 60, "uname": "nosqlfan60 ″}
Simple comparison table
SQL Statement Mongo Query Language Statement
Create table users (a Number, B Number) implicit; can be done explicitly
Insert into users values (1, 1) db. users. insert ({a: 1, B: 1 })
SELECT a, B FROM users db. users. find ({}, {a: 1, B: 1 })
SELECT * FROM users db. users. find ()
SELECT * FROM users WHERE age = 33 db. users. find ({age: 33 })
SELECT a, B FROM users WHERE age = 33 db. users. find ({age: 33}, {a: 1, B: 1 })
SELECT * FROM users WHERE age = 33 order by name db. users. find ({age: 33}). sort ({name: 1 })
SELECT * FROM users WHERE age> 33 db. users. find ({'age' :{$ gt: 33 }})})
SELECT * FROM users WHERE age <33 db. users. find ({'age' :{$ lt: 33 }})})
SELECT * FROM users WHERE name LIKE "% Joe %" db. users. find ({name:/Joe

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.