技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> MySQL --> MySQL MongoDB SQL 对应

MySQL MongoDB SQL 对应

浏览:2630次  出处信息

FROM: SQL to Mongo Mapping Chart

  SQL statement Mongo statement
DDL CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, a INT, b INT, age INT, name CHAR(32)) implicit; can also be done explicitly with
db.createCollection("user1")
ALTER TABLE users ADD ... implicit
DROP TABLE users db.users.drop()
CREATE DATABSE db_name implicite
SHOW DATABASES show dbs
USE db_name use db_name
SHOW TABLES show collections
CREATE INDEX idx_name ON users(name) db.users.ensureIndex({name : 1})
CREATE INDEX idx_name_ts ON users(name, ts DESC) db.users.ensureIndex({name : 1, ts : -1})
DROP INDEX idx_name ON users db.users.dropIndex({name : 1, ts : -1})
SHOW INDEXES FROM users db.users.getIndexes()
DML INSERT INTO users VALUES(3, 5) db.users.insert({a: 3, b: 5})
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 ASC db.users.find({age : 33}).sort({name : 1})
SELECT * FROM users ORDER BY name DESC db.users.find().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 : {$ne : 33}})
SELECT * FROM users WHERE age > 33 AND age <= 44 db.users.find({age : {$gt: 33, $lte : 44}})
SELECT * FROM users WHERE name LIKE "%Joe%" db.users.find({name : /Joe/})
SELECT * FROM users WHERE id IN (3, 4, 5) db.users.find({id, {$in : [3, 4, 5]}})
SELECT * FROM users WHERE id NOT IN (3, 4, 5) db.users.find({id, {$nin : [3, 4, 5]}})
SELECT * FROM users WHERE id = 2 db.users.find({id , {$all : [2]}})
SELECT * FROM users WHERE a = 1 AND name = 'Joe' db.users.find({ a : 1, name : 'Joe'})
SELECT * FROM users WHERE a = 1 OR b = 2 db.users.find({$or : [{a : 1}, {b : 2}]})
SELECT * FROM users WHERE name LIKE "Joe%" db.users.find({name : /^Joe/})
SELECT * FROM users WHERE name LIKE "%Joe" db.users.find({name : /Joe$/})
SELECT * FROM users LIMIT 20, 10 db.users.find().limit(10).skip(20)
SELECT * FROM users LIMIT 1 db.users.findOne()
SELECT id FROM users u , users_extend e WHERE u.user_id = e.user_id AND e.c = 12345 db.users.find({"users_extend.c" : 12345}, {_id : 1})
SELECT customer.name FROM customers, orders WHERE orders.id = "q139" AND orders.custid = customers.id var o = db.orders.findOne({_id : "q139"})
var name = db.customers.findOne({_id : o.custid})
EXPLAIN SELECT * FROM users WHERE a = 3 db.users.find({a : 3}).explain()
UPDATE users SET a = 1 WHERE b = 2 db.users.update({b : 2}, {$set : {a : 1}}, false, true)
UPDATE users SET a = a + 2 WHERE b = 2 db.users.update({b : 2}, {$inc: {a : 2}}, false, true)
DELETE FROM users WHERE a = 3 db.users.remove({ a : 3})
DELETE FROM users db.users.remove()

MongoDB 中可用到的其它语句:

  • 存在某字段: db.users.find({a : {$exists : true}})
  • 不存在某字段: db.users.find({a : {$exists : false}})
  • 查询指定长度: db.users.find({name : {$size : 10}})
  • 查询字段是数组: db.users.find({name.first; : 'Joe', name.last : 'David'})
  • var cursor = db.users.find()
    while(cursor.hasNext()) printjson(curson.next())
  • db.users.find().forEach(printjson)
  • db.users.find().toArray()

建议继续学习:

  1. MongoDB与内存    (阅读:5347)
  2. Nodejs和MongoDB初体验    (阅读:5021)
  3. SQL里是否可以使用JOIN    (阅读:4932)
  4. 我为什么选择MongoDB    (阅读:3878)
  5. 白话MongoDB(一)    (阅读:3632)
  6. MySQL和MongoDB设计实例对比    (阅读:3633)
  7. 记一次MongoDB性能问题    (阅读:3399)
  8. 也来玩玩MongoDB    (阅读:3317)
  9. MySQL Cluster 与 MongoDB 复制及分片设计及原理    (阅读:3214)
  10. 在MongoDB中模拟auto_increment    (阅读:2993)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1