MySQL | MongoDb |
---|---|
Tables | Collections |
Rows | Documents |
Columns | Fields |
SHOW TABLES | db.getCollectionNames() |
SELECT * FROM users | db.users.find() |
SELECT name, weight FROM users | db.users.find({}, {name: 1, weight: 1, _id: 0})
Note: By default _id will be included in the result |
SELECT COUNT(*) FROM users | db.users.count() |
SELECT COUNT(*) FROM users where weight > 50 | db.users.count({weight: {$gt: 50}}) |
SELECT * FROM users order by name ASC | db.users.find().sort({name: 1}) |
SELECT * FROM users order by name DESC | db.users.find().sort({name: -1}) |
INSERT INTO users (username, name, gender, weight, job) values('aurora1001', 'Aurora', 'F', 45, 'Teaching') | db.users.insert({username: 'aurora1001', name: 'Aurora', gender: 'F', weight: 45, 'job' : 'Teaching'}) |
SELECT * FROM users WHERE gender = 'M' AND weight > 70 | db.users.find({gender: 'M', weight: {$gt: 70}}) |
SELECT * FROM users WHERE gender != 'F' AND weight >= 70 | db.users.find({gender: {$ne: 'F'}, weight: {$gte: 70}}) |
SELECT * FROM users WHERE job IN ('Teaching', 'Sales') | db.users.find({job: {$in:['Teaching','Sales']}}) |
SELECT * FROM users WHERE gender = 'F' AND (job = 'Teaching' OR weight <= 50) | db.users.find({gender: 'F', $or: [{job: 'Teaching'}, {weight: {$lte: 50}}]}) |
UPDATE users SET status = 'ACTIVE' | db.users.update({}, {$set: {status: 'ACTIVE' }}, {multi: true }); |
UPDATE users SET weight=50 WHERE name='Aurora' | db.users.update({name: 'Aurora'}, {$set: {weight: 50}}, {multi: true }) |
UPDATE users SET weight = weight + 5 WHERE name='Aurora' | db.users.update({name: 'Aurora'}, {$inc: {weight: 5}}, {multi: true }) |
UPDATE users SET weight = weight - 5 WHERE name='Aurora' | db.users.update({name: 'Aurora'}, {$inc: {weight: -5}}, {multi: true }) |
INSERT INTO users(username, weight) VALUES("aurora1001", 50) ON DUPLICATE KEY UPDATE weight = 60
Note: username has UNIQUE index |
db.users.update({"username": 'aurora1001'}, {$set: {weight: 60}}, {upsert: true }) |
Thursday, December 10, 2015
MySQL Vs MongoDb
Subscribe to:
Posts (Atom)