基本概念對比
mysql
SQL Terms/Concepts | MongoDB Terms/Concepts |
---|---|
database | database |
table | collection |
row | document or BSON document |
column | field |
index | index |
table joins | embedded documents and linking |
primary keysql Specify any unique column or column combination as primary key.mongodb |
In MongoDB, the primary key is automatically set to the_id field.oracle |
aggregation (e.g. group by) | aggregation pipelineapp See the SQL to Aggregation Mapping Chart.測試 |
執行spa
The following table presents the MySQL/Oracle executables and the corresponding MongoDB executables..net
MySQL/Oracle | MongoDB | |
---|---|---|
Database Server | mysqld/oracle | mongod |
Database Client | mysql/sqlplus | mongo |
sql表結構 users
id user_id age status
1 "abc123" 55 'A'code
mongo集合結構:
{ _id: ObjectId("509a8fb2f3f4948bd2f983a0"), user_id: "abc123", age: 55, status: 'A'}
因爲mongodb沒有字段結構的Schema,字段類型能夠很方便改變或增長,若是要查找沒有存在某個字段的數據,能夠db.users.find({age:{$exists:false}})
SQL Schema Statements | MongoDB Schema Statements |
---|---|
CREATE TABLE users ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id)) |
db.users.insert( { user_id: "abc123", age: 55, status: "A" } ) 或者 db.createCollection("users") |
ALTER TABLE usersADD join_date DATETIME |
db.users.update({ }, { $set:{ join_date: new Date() } }, { multi: true }) |
ALTER TABLE usersDROP COLUMN join_date |
db.users.update({ }, { $unset: { join_date: "" } }, { multi: true }) |
CREATE INDEX idx_user_id_asc ON users(user_id) |
db.users.ensureIndex( { user_id: 1 } ) |
CREATE INDEX idx_user_id_asc_age_desc ON users(user_id, age DESC) |
db.users.ensureIndex( { user_id: 1, age: -1 } ) |
DROP TABLE users |
db.users.drop() |
mongodb包含了等值測試、排序及範圍過濾查詢的組合索引創建方法:
1. 等值測試
在索引中加入全部須要作等值測試的字段,任意順序。
2. 排序字段(多排序字段的升/降序問題 )
根據查詢的順序有序的向索引中添加字段。
3. 範圍過濾
以字段的基數(Collection中字段的不一樣值的數量)從低到高的向索引中添加範圍過濾字段。
還有一個規則:若是索引中的等值或者範圍查詢字段不能過濾出Collection中90%以上的文檔,那麼把它移除索引估計會更好一些。而且若是你在一個Collection上有多個索引,那麼必須hint Mongos。
http://www.csdn.net/article/2012-11-09/2811690-optimizing-mongodb-compound
插入
SQL INSERT Statements | MongoDB insert() Statements |
---|---|
INSERT INTO users(user_id, age, status)VALUES ("bcd001", 45, "A") |
db.users.insert( {user_id:"bcd001", age:45, status:"A"} ) |
基本查詢
SQL SELECT Statements | MongoDB find() Statements |
---|---|
SELECT * FROM users |
db.users.find() |
SELECT id, user_id, status FROM users |
db.users.find({ }, { user_id: 1, status: 1 }) |
SELECT user_id, status FROM users |
db.users.find({ }, { user_id: 1, status: 1, _id: 0 }) |
SELECT *FROM users WHERE status = "A" |
db.users.find({ status: "A" }) |
SELECT user_id, status FROM users WHERE status = "A" |
db.users.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 }) |
SELECT * FROM users WHERE status != "A" |
db.users.find({ status: { $ne: "A" } }) |
SELECT * FROM users WHERE status = "A" AND age = 50 |
db.users.find({ status: "A", age: 50 }) |
SELECT *FROM users WHERE status = "A" OR age = 50 |
db.users.find({ $or: [ { status: "A" } , { age: 50 } ] }) |
SELECT * FROM users WHERE age > 25 |
db.users.find({ age: { $gt: 25 } }) |
SELECT * FROM users WHERE age < 25 |
db.users.find({ age: { $lt: 25 } }) |
SELECT * FROM users WHERE age > 25 AND age <= 50 |
db.users.find({ age: { $gt: 25, $lte: 50 } }) |
SELECT * FROM users WHERE user_id like "%bc%" |
db.users.find({ user_id: /bc/ }) |
SELECT * FROM users WHERE user_id like "bc%" |
db.users.find({ user_id: /^bc/ }) |
SELECT * FROM users WHERE status = "A" ORDER BY user_id ASC |
db.users.find({ status: "A" } ) .sort( { user_id: 1 } ) |
SELECT * FROM users WHERE status = "A" ORDER BY user_id DESC |
db.users.find({ status: "A" } ) .sort( { user_id: -1 } ) |
SELECT COUNT(*) FROM users |
db.users.count() or db.users.find().count() |
SELECT COUNT(user_id) FROM users |
db.users.count( { user_id: { $exists: true } } ) or db.users.find( { user_id: { $exists: true } } ) .count() |
SELECT COUNT(*) FROM users WHERE age > 30 |
db.users.count( { age: { $gt: 30 } } ) or db.users.find( { age: { $gt: 30 } } ).count() |
SELECT DISTINCT(status) FROM users |
db.users.distinct( "status" ) 若是distinct多個字段,使用使用聚合操做 |
SELECT * FROM usersLIMIT 1 |
db.users.findOne() or db.users.find().limit(1) |
SELECT * FROM users LIMIT 5 SKIP 10 |
db.users.find().limit(5).skip(10) |
EXPLAIN SELECT * FROM users WHERE status = "A" |
db.users.find( { status: "A" } ).explain() |
mongodb裏的操做符
(1) $gt > (大於)
(2) $lt < (小於)
(3) $gte >= (大於等於)
(4) $lt <= (小於等於)
(5) $ne != (不等於)
(6) $in in (包含)
(7) $nin not in (不包含)
(8) $exists exist (字段是否存在)
(9) $inc 對一個數字字段field增長value(該字段必須有數字值)
(10) $set 就是至關於sql的set field = value
(11) $unset 就是刪除字段
(12) $push 把value追加到field裏面去,field必定要是數組類型才行,若是field不存在,會新增一個數組類型加進去
(13) $pushAll 同$push,只是一次能夠追加多個值到一個數組字段內
(14) $addToSet 增長一個值到數組內,並且只有當這個值不在數組內才增長。
(15) $pop 刪除最後一個值:{ $pop : { field : 1 } }刪除第一個值:{ $pop : { field : -1 } }注意,只能刪除一個值,也就是說只能用1或-1,而不能用2或-2來刪除兩條。
(16) $pull 從數組field內刪除一個等於value值
(17) $pullAll 同$pull,能夠一次刪除數組內的多個值
(18) $ 操做符 自身的意思,表明按條件找出的數組裏面某項自身數據。
複雜聚合查詢group by,參考aggregate
更新記錄
SQL Update Statements | MongoDB update() Statements |
---|---|
UPDATE users SET status = "C" WHERE age > 25 |
db.users.update( { age: { $gt: 25 } }, { $set: { status: "C" } }, { multi: true }) |
UPDATE users SET age = age + 3 WHERE status = "A" |
db.users.update( { status: "A" } , { $inc: { age: 3 } }, { multi: true }) |
刪除記錄
SQL Delete Statements | MongoDB remove() Statements |
---|---|
DELETE FROM usersWHERE status = "D" |
db.users.remove( { status: "D" } ) |
DELETE FROM users |
db.users.remove( ) |
參考