2010年10月5日 星期二

mongoDB與MySQL 指令比較

閱讀紀錄
MySQL Program → Mongo Program
  • mysqld  → mongod (啟動DB)
  • mysql    → mongo    (DB連線,進入 Shell操作模式)

MySQL Statement → Mongo Statement
插入 
  • INSERT INTO users VALUES(2,3) →
    db.users.insert({a:2,b:3})

搜尋全部
  • SELECT * FROM users →
    db.users.find()
SELECT0
  • SELECT a,b FROM users →
    db.users.find({},{a:2,b:3})
SELECT1
  • SELECT * FROM users WHERE age=30 →
    db.users.find({age:30})
SELECT2
  • SELECT a,b FROM users WHERE age=30 →
    db.users.find({age:33},{a:2,b:3})


SELECT3
  • SELECT * FROM users WHERE age=30 ORDER BY name → db.users.find({age:33}).sort({name:1})
SELECT4
  • SELECT * FROM users WHERE age>30 →
    db.users.find({'age':{$gt:30}})
SELECT5
  • SELECT * FROM users WHERE age<30 →
    db.users.find({'age':{&lt:30}})
SELECT6
  • SELECT * FROM users ORDER BY name DESC → db.users.find().sort({name:-1})
建立索引
  • CREATE INDEX myindexname ON users(name) → db.users.ensureIndex({name:1})
SELECT命令7
  • SELECT * FROM users WHERE a=1 and b='q' → db.users.find({a:1,b:'q'})
SELECT命令8
  • SELECT * FROM users LIMIT 10 SKIP 20 → db.users.find.limit(10).skip(20)
SELECT命令9
  • SELECT * FROM users LIMIT 1 → db.users.findOne()
SELECT命令10
  • EXPLAIN SELECT * FROM users WHERE z=3 → db.users.find({z:3}).explain()
SELECT命令11
  • SELECT DISTINCT last_name FROM users → db.users.distinct('last_name')
SELECT命令12
  • SELECT COUNT(*y) FROM users → db.users.count()
SELECT命令13
  • SELECT COUNT(*) FROM users where AGE > 30 → db.users.find({age:{'&gt':30}}).count()
SELECT命令14
  • SELECT COUNT(AGE) FROM users → db.users.find({age:{'$exists':true}}).count()
SELECT命令15
  • UPDATE users SET a=1 WHERE b='q' → db.users.update({b:'q'},{$set:{a:1}},false,true)

沒有留言:

張貼留言