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)

MongoDB Shell 下的 基本操作指令

(文章來自mongoDB mongo - The interactive章節)
//以下命令都是進入mongoDB Shell環境下使用
//進入Shell用的Linux命令 ./bin/mongo


Special Command Helpers
Non-javascript convenience macros:
Show help
顯示最上層的幫助文件
  • >help
Show help on db methods
顯示(資料庫)db系列命令的幫助文件
  • >db.help()
Show help on collection methods
顯示(集合)myColl系列命令的幫助文件
  • >db.myColl.help()
Print a list of all databases on this server
查詢mongoDB上所有的資料庫
  • >show dbs



Set the db variable to represent usage of dbname on the server
切換資料庫
  • >use [db_name]
Print a list of all collections for current database
顯示所有的collections (mongoDB的collection 類似 MySQL的table)
  • >show collections
Print a list of users for current database
列出當前資料庫用戶
  • >show users
Print most recent profiling operations that took >= 1ms
列出最近的操作?
  • >show profile
Basic Shell Javascript Operations

//未完

MongoDB Starting the Shell

啟動mongodb的Shell環境
  • ./bin/mongo

connects to the [database] on your local machine.
連接本地端機器上的database。
  • ./mongo [database Name]
  • ./mongo testdb


connects to the [database] on 192.168.11.3

指定連接特定位址上的database
  • ./mongo [IP Address]/[database]
  • ./mongo 192.168.11.3/testdb


connects to the foo database on [domainName]
指定連接domainName上的database。
  • ./mongo [domainName]/[database]
  • ./mongo dbserver.mydomain.com/testdb

connects to the [database] on [IP Address] on [port number]
指定IP位址與使用Port number。
  • ./mongo [IP Address]:[Port Number]/[database]
  • ./mongo 192.168.11.3:7000/testbase