This article describes the common SQL statements in MySQL in MongoDB, if you use MySQL for a long time to be tempted to MongoDB, this simple article can help you get into the role faster.
Inquire:
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 you just need:
Db.user.insert ({' name ': ' Starlee ', ' age ': +, ' email ': ' [email protected] '})
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 ' = * * WHERE ' name ' = ' Starlee '
Mongo:
Db.user.update ({' name ': ' Starlee '}, {$set: {' age ': 36}})
Mysql:
UPDATE user SET ' age ' = ' age ' + 3 WHERE ' name ' = ' Starlee '
Mongo:
Db.user.update ({' name ': ' Starlee '}, {$inc: {' Age ': 3}})
Mysql:
SELECT COUNT (*) from user WHERE ' name ' = ' Starlee '
Mongo:
Db.user.find ({' name ': ' Starlee '}). Count ()
Mysql:
SELECT * from user limit 10,20
Mongo:
Db.user.find (). Skip. 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 the user where name= ' foo ' 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})
Found a lot of people in the search MongoDB loop Insert data, below the MongoDB loop to insert the data method is added below:
for (Var i=0;i<100;i++) Db.test.insert ({uid:i,uname: ' Nosqlfan ' +i});
The above inserts 100 data at once, the approximate structure is as follows:
{"_id": ObjectId ("4c876e519e86023a30dde6b8″)," UID ":," uname ":" Nosqlfan55″}
{"_id": ObjectId ("4c876e519e86023a30dde6b9″"), "UID": "uname": "Nosqlfan56″}
{"_id": ObjectId ("4c876e519e86023a30dde6ba"), "UID": page, "uname": "Nosqlfan57″}
{"_id": ObjectId ("4C876E519E86023A30DDE6BB"), "UID": +, "uname": "Nosqlfan58″}
{"_id": ObjectId ("4C876E519E86023A30DDE6BC"), "UID":, "uname": "Nosqlfan59″}
{"_id": ObjectId ("4C876E519E86023A30DDE6BD"), "UID": $, "uname": "Nosqlfan60″}
MySQL and MONGO DB syntax comparison