> show dbs admin 0.000GB local 0.000GB pmall 0.000GB
沒有數據的數據庫是不顯示的正則表達式
> db test
若是指定的庫名存在則切換,不存在則建立sql
> use pmall switched to db pmall
> db.dropDatabase() { "dropped" : "pmall_test", "ok" : 1 }
> use admin switched to db admin > db.auth("root","111111") 1
用戶會所有建立在admin庫的system.users集合中mongodb
> use pmall switched to db pmall > db.createUser( ... { ... user:"imlichao", ... pwd:"111111", ... roles:["read","readWrite","dbAdmin","userAdmin"] ... } ... ) Successfully added user: { "user" : "imlichao", "roles" : [ "read", "readWrite", "dbAdmin", "userAdmin" ] }
權限解釋數據庫
read:容許用戶讀取指定數據庫 readWrite:容許用戶讀寫指定數據庫 dbAdmin:容許用戶在指定數據庫中執行管理函數,如索引建立、刪除,查看統計或訪問system.profile userAdmin:容許用戶向system.users集合寫入,能夠找指定數據庫裏建立、刪除和管理用戶 clusterAdmin:只在admin數據庫中可用,賦予用戶全部分片和複製集相關函數的管理權限。 readAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的讀權限 readWriteAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的讀寫權限 userAdminAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的userAdmin權限 dbAdminAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的dbAdmin權限。 root:只在admin數據庫中可用。超級帳號,超級權限 |
> db.createCollection("myuser") { "ok" : 1 }
> db.createCollection("myuser") { "ok" : 1 } > db.myuser.drop() true
> show collections myuser system.users
> db.myuser.find() { "_id" : ObjectId("5b30a7ffec543cee8e9e70e3"), "username" : "imlichao", "password" : "111111" } { "_id" : ObjectId("5b30a8dcec543cee8e9e70e5"), "username" : "imlichao1", "password" : "111111" } { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
> db.myuser.insert( ... { ... username:"imlichao", ... password:"111111" ... } ... ) WriteResult({ "nInserted" : 1 })
若是collection name是一個不存在的集合,則會自動建立這個集合。函數
語法格式:編碼
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
參數說明:spa
更新找到的第一個記錄code
> db.myuser.update( ... { ... "username":"imlichao" ... }, ... { ... $set:{"password":"222222"} ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
更新所有記錄 對象
> db.myuser.update( ... { ... "username":"imlichao" ... }, ... { ... $set:{"password":"222222"} ... }, ... { ... multi:true ... } ... ) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
save() 方法經過傳入的文檔來替換已有文檔。blog
語法格式:
db.collection.save( <document>, { writeConcern: <document> } )
參數說明:
> db.myuser.save( ... { ... "_id":ObjectId("5b3190330d9d1b3044553c1a"), ... "username":"miaomiao", ... "password":"888888" ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.myuser.deleteOne( ... { ... "username":"imlichao" ... } ... ) { "acknowledged" : true, "deletedCount" : 1 }
> db.myuser.deleteMany( ... { ... "username":"imlichao1" ... } ... ) { "acknowledged" : true, "deletedCount" : 1 }
語法格式:
db.collection.find(query, projection)
條件查詢語法:
若是你熟悉常規的 SQL 數據,經過下表能夠更好的理解 MongoDB 的條件語句查詢:
操做 | 格式 | 範例 | RDBMS中的相似語句 |
---|---|---|---|
等於 | {<key>:<value> } |
db.col.find({"by":"菜鳥教程"}).pretty() |
where by = '菜鳥教程' |
小於 | {<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
小於或等於 | {<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
大於 | {<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
大於或等於 | {<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
不等於 | {<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
與 | {<key>:<value>,<key>:<value> } |
db.col.find({"by":"a", "by1":"b"}).pretty() | where by ='a'and by1 ='b' |
或 | {$or:[{<key>:<value> }, {<key>:<value> }]} |
db.col.find({$or:[{"by":"a"},{"by1": "b"}]}).pretty() | where by ='a'or by1 ='b' |
類型查詢:
$type操做符是基於BSON類型來檢索集合中匹配的數據類型,並返回結果。
> db.myuser.find({"username" : {$type : 2}}).pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" }
類型及編碼對應:
型 | 數字 | 備註 |
---|---|---|
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 6 | 已廢棄。 |
Object id | 7 | |
Boolean | 8 | |
Date | 9 | |
Null | 10 | |
Regular Expression | 11 | |
JavaScript | 13 | |
Symbol | 14 | |
JavaScript (with scope) | 15 | |
32-bit integer | 16 | |
Timestamp | 17 | |
64-bit integer | 18 | |
Min key | 255 | Query with -1. |
Max key | 127 |
正則表達式(模糊查詢):
使用$regex操做符
> db.myuser.find({ ... "username":{$regex:"iM",$options:"$i"} ... }).pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
$options的$i表明不區分大小寫
不使用$regex操做符
> db.myuser.find({ ... "username":/iM/i ... }).pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
插入變量
正則表達式中使用變量。必定要使用eval將組合的字符串進行轉換,不能直接將字符串拼接後傳入給表達式。不然沒有報錯信息,只是結果爲空!實例以下:
var name=eval("/" + 變量值key +"/i");
如下是模糊查詢包含title關鍵詞, 且不區分大小寫:
title:eval("/"+title+"/i") // 等同於 title:{$regex:title,$Option:"$i"}
提取字段:
若不指定 projection,則默認返回全部鍵,指定 projection 格式以下,有兩種模式
db.collection.find(query, {title: 1, by: 1}) // inclusion模式 指定返回的鍵,不返回其餘鍵 db.collection.find(query, {title: 0, by: 0}) // exclusion模式 指定不返回的鍵,返回其餘鍵
_id 鍵默認返回,須要主動指定 _id:0 纔會隱藏
兩種模式不可混用(由於這樣的話沒法推斷其餘鍵是否應返回)
db.collection.find(query, {title: 1, by: 0}) // 錯誤
只能全1或全0,除了在inclusion模式時能夠指定_id爲0
db.collection.find(query, {_id:0, title: 1, by: 1}) // 正確
pretty() 方法以格式化的方式來顯示全部文檔。
> db.myuser.find().pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" }
> db.myuser.find().limit(2) { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
> db.myuser.find().skip(2) { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" }
正序使用1,倒序使用-1
> db.myuser.find().sort({"_id":1}) { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" } > db.myuser.find().sort({"_id":-1}) { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" }
經過在字段上建立索引能夠提升數據庫查詢和排序效率。
> db.test1.createIndex({"username":1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
建立正序索引使用1,倒序索引使用-1。排序時規則跟索引一致,此索引纔會生效。例如索引爲1正序索引,只有在正序排序時纔有效。
咱們可使用多個字段建立聯合索引
> db.test1.createIndex({"username":1,"password":-1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1 }
> db.test1.getIndexes() [ { "v" : 2, "key" : { "username" : 1 }, "name" : "username_1", "ns" : "pmall.test1" }, { "v" : 2, "key" : { "username" : 1, "password" : -1 }, "name" : "username_1_password_-1", "ns" : "pmall.test1" } ]
> db.test1.dropIndex("username_-1") { "nIndexesWas" : 4, "ok" : 1 }