基本操作:
幫助
help
KEYWORDS.help
KEYWORDS.[TAB]
不同級別和命令
db 數(shù)據(jù)庫管理操作有關(guān) db.help()
sh 和shard分片有關(guān)的命令 sh.help()
rs 復(fù)制集管理有關(guān)的命令 rs.help()
常用操作查看當(dāng)前db版本
test> db.version()
3.2.6
顯示當(dāng)前數(shù)據(jù)庫
test>db
test
或
>db.getName()
test
查詢所有數(shù)據(jù)庫
test> show dbs (==show databases)
local 0.000GB
切換數(shù)據(jù)庫
> use local
switched to db local
顯示當(dāng)前數(shù)據(jù)庫狀態(tài),查看local數(shù)據(jù)
test> use local
switched to db local
local> db.stats()
查看當(dāng)前數(shù)據(jù)庫的連接機器地址
> db.getMongo()
connection to 127.0.0.1
指定數(shù)據(jù)庫進行連接:(默認連接本機test數(shù)據(jù)庫)
# mongo 192.168.1.24/admin
[mongod@mongodb ~]$ mongo 192.168.1.24/admin
MongoDB shell version: 3.2.6
connecting to: 192.168.1.24/admin
admin>
創(chuàng)建數(shù)據(jù)庫:當(dāng)use的時候巩趁,系統(tǒng)就會自動創(chuàng)建一個數(shù)據(jù)庫舟陆。如果use之后沒有創(chuàng)建任何集合消返。系統(tǒng)就會刪除這個數(shù)據(jù)庫丸冕。
--刪除數(shù)據(jù)庫:(如果沒有選擇任何數(shù)據(jù)庫歹撒,會刪除默認的test數(shù)據(jù)庫)
刪除test數(shù)據(jù)庫
test> show dbs
local 0.000GB
test 0.000GB
test> use test
switched to db test
test> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
集合操作(表操作)
-
創(chuàng)建集合
-
方法1:手動創(chuàng)建
admin>use app switched to db app app>db.createCollection('a') { "ok" : 1 } app>db.createCollection('b') { "ok" : 1 } >show collections //查看當(dāng)前數(shù)據(jù)下的所有集(==show tables) a b 或 db.getCollectionNames() [ "a", "b" ]
-
方法2:當(dāng)插入一個文檔的時候拉岁,一個集合就會自動創(chuàng)建铣墨。
admin> use app switched to db app app>db.c.insert({username:"mongodb"}) WriteResult({ "nInserted" : 1 }) app> show collections a b c app> db.c.find() { "_id" : ObjectId("5743c9a9bf72d9f7b524713d"), "username" : "mongodb" } **或者使用db.c.find().pretty()來優(yōu)化顯示效果**
-
-
刪除集合
app> use app switched to db app app> db.log.drop() //刪除集合
重命名集合
把log改名為log1
app> db.log.renameCollection("log1")
{ "ok" : 1 }
app> show collections
a
b
c
log1
-
for循環(huán)插入數(shù)據(jù)
app> for(i=0;i<100;i++){ db.log.insert({"uid":i,"name":"mongodb","age":6,"date":new Date()}); }
-
查詢集合中的記錄數(shù)
app> db.log.find() //查詢所有記錄
注:默認每頁顯示20條記錄,當(dāng)顯示不下的的情況下级解,可以用it迭代命令查詢下一頁數(shù)據(jù)。
設(shè)置每頁顯示數(shù)據(jù)的大刑锇蟆:
DBQuery.shellBatchSize=50; //每頁顯示50條記錄app> db.log.find({uid:'50'}) //查詢uid位50的記錄勤哗,相當(dāng)于mysql的where條件 app> db.log.findOne() //查看第1條記錄 app> db.log.count() //查詢總的記錄數(shù)
-
刪除集合中的記錄數(shù)
app> db.log.remove({}) //刪除集合中所有記錄 db.log.distinct("name") //查詢?nèi)サ舢?dāng)前集合中某列的重復(fù)數(shù)據(jù): [ "mm" ]
-
查看集合存儲信息
app>db.log.stats() app> db.log.dataSize() //集合中數(shù)據(jù)的原始大小 app> db.log.totalIndexSize() //集合中索引數(shù)據(jù)的原始大小 app>db.log.totalSize() //集合中索引+數(shù)據(jù)壓縮存儲之后的大小 app> db.log.storageSize() //集合中數(shù)據(jù)壓縮存儲的大小
SQL語言與CRUD語言對照
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)) | Implicitly created on firstinsert()operation. The primary key_idisautomatically added if_idfield is not specified.db.users.insert( {user_id: "abc123",age: 55,status: "A"} ) However, you can also explicitly create a collection: db.createCollection("users") |
ALTER TABLE users ADD join_dateDATETIME | 在Collection 級沒有數(shù)據(jù)結(jié)構(gòu)概念。然而在document級掩驱,可以通過 |
ALTER TABLE usersDROP COLUMN join_date | 在Collection 級沒有數(shù)據(jù)結(jié)構(gòu)概念。然而在document級欧穴,可以通過 |
CREATE INDEX idx_user_id_asc ON users(user_id) | db.users.createIndex( { user_id: 1 } ) |
CREATE INDEX idx_user_id_asc_age_desc ON users(user_id, age DESC) | db.users.createIndex( { user_id: 1, age: -1 } ) |
DROP TABLE users | db.users.drop() |
插入語句 | |
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, _id: 0 }) |
SELECT user_id, status FROM users | db.users.find({ },{ user_id: 1, status: 1 }) |
SELECT * FROM usersWHERE 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 }) |
數(shù)據(jù)更新操作 | |
SQL Update Statements | MongoDB update() Statements |
UPDATE users SET status = "C" WHERE age > 25 | db.users.update({ age: { |
UPDATE users SET age = age + 3 WHERE status = "A" | db.users.update({ status: "A" } ,{ $inc: { age: 3 } },{ multi: true }) |
數(shù)據(jù)庫刪除操作 | |
DELETE FROM usersWHERE status = "D" | db.users.remove( { status: "D" } ) |
DELETE FROM users | db.users.remove({}) |
用戶權(quán)限管理
創(chuàng)建管理員用戶
-
(1)創(chuàng)建用戶
use admin db.createUser( { user: "root", pwd: "root", roles: [ { role: "root", db: "admin" } ] } )
注意:
1、創(chuàng)建管理員角色用戶的時候涮帘,必須要到admin下創(chuàng)建
2拼苍、刪除的也要到相應(yīng)的庫下操作use admin db.dropuser('root');
-
(2)驗證
db.auth("root","root")
-
(3)配置文件開啟auth驗證
cat >>mongodb.conf<<EOF security: authorization: enabled EOF
-
(4)重啟mongodb
測試用戶可用性:[mongod@db01 ~]$ mongo -uroot -proot admin
注意:用戶在哪個數(shù)據(jù)庫下創(chuàng)建的,最后加上什么庫,數(shù)據(jù)庫內(nèi)驗證:
[mongod@db01 ~]$ mongo > use admin > db.auth("root","root")
2调缨、按生產(chǎn)需求創(chuàng)建應(yīng)用用戶
-
例1.創(chuàng)建對某庫的只讀用戶--test
-
創(chuàng)建對某庫的只讀用戶
use test db.createUser( { user: "test", pwd: "test", roles: [ { role: "read", db: "test" } ] } ) db.auth("test","test") show users;
-
登錄test用戶疮鲫,并測試
db.createCollection('b')
-
-
例2.創(chuàng)建某庫的讀寫用戶
db.createUser( { user: "test1", pwd: "test1", roles: [ { role: "readWrite", db: "test" } ] } ) show users; db.auth("test1","test1")
-
例3.創(chuàng)建 分別對多庫的不同權(quán)限的用戶
use app db.createUser( { user: "app03", pwd: "app03", roles: [ { role: "readWrite", db: "app", } ] } )
例4.刪除用戶
刪除app01用戶:先登錄到admin數(shù)據(jù)庫
mongo -uroot –proot 192.168.1.24/admin
use app
db.dropUser("app01")