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