SQL与NoSQL的CRUD对照

flyfish 2015-7-21

Create, Read, Update and Delete (CRUD)

SQL方式



SELECT 列名称 FROM 表名称
SELECT * FROM 表名称
SELECT 列名称 FROM 表名称 WHERE 列 运算符 值


INSERT INTO 表名称 VALUES (值1, 值2,....)
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)


DELETE FROM 表名称 WHERE 列名称 = 值


UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值

NoSQL方式(MongoDB)


db.collection.find(query, projection)
db.products.find( { qty: { $gt: 25 } } )


db.collection.insert()
db.products.insert( { item: "card", qty: 15 } )


db.collection.remove()
db.products.remove( { qty: { $gt: 20 } } )


db.collection.update(query, update, options)

db.people.update(
   { name: "Andy" },
   {
      name: "Andy",
      rating: 1,
      score: 1
   },
   { upsert: true }
)

对照 

SELECT * FROM users WHERE age=18
db.users.find({age:18})

INSERT INTO USERS VALUES('sss',18) 
db.users.insert({name:'sss',age:18})

UPDATE users SET age=18 WHERE name='sss' 
db.users.update({name:'sss'}, {$set:{age:18}}, false, true)

DELETE FROM users WHERE age=18
db.users.remove({age:18});