這邊記錄一下 mongodb command line mode 的基楚操作指令 從無到有的 建立 collections 新增 查詢 更新 刪除 等基本指令,我的環境是 MongoDB shell version v3.6.3 指令可能會因不同版本有些微的差異。
mongodb 當資料庫啟用驗證模式後 每個db都需要有對應的 帳密登入
首先登入超級管理者帳戶
mongo mongodb://super:superpwd@172.1.2.3:27017/admin
登入後切換到你要新增的db
use op_log
切換成功會顯示
switched to db op_log
再下新增使用者的指令
db.createUser({user:"op",pwd:"test_pwd",roles:[{role:"readWrite",db:"op_log"}]})
新增成功會顯示
Successfully added user: {
“user” : “op”,
“roles” : [
{
“role” : “readWrite”,
“db” : “op_log”
}
]
}
若建錯帳號也可以使用刪除使用者指令
db.runCommand( { dropUser: "op" } )
再來就可以使用新的帳密去登入新的db
mongo mongodb://op:test_pwd@172.1.2.3:27017/op_log
登入成功會顯示
MongoDB shell version v3.6.3
connecting to: mongodb://172.31.24.1:27017/op_log
MongoDB server version: 3.6.3
新增一筆資料 指令
db.op_log.insert({"acount":"test"})
新增成功會顯示
WriteResult({ “nInserted” : 1 })
查詢 指令
db.op_log.find()
查詢成功會顯示
{ “_id” : ObjectId(“5e9d2f4eee592e24d2cdc2c7”), “acount” : “test” }
新增多筆資料 指令
db.op_log.insertMany([{"account":"test1"},{"account":"test2"}])
新增成功顯示
{
“acknowledged” : true,
“insertedIds” : ObjectId(“5e9d38579afa78b4837efc98”),
ObjectId(“5e9d38579afa78b4837efc99”)
}
查詢 db.op_log.find()
{ “_id” : ObjectId(“5e9d2f4eee592e24d2cdc2c7”), “acount” : “test” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc98”), “account” : “test1” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc99”), “account” : “test2” }
這邊會發現白痴的小編打錯字 account 打成 acount, 這邊我們透過
save指令來修改整筆資料
db.op_log.save({"_id":ObjectId("5e9d2f4eee592e24d2cdc2c7"),"account":"test"})
成功會顯示
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
查詢 db.op_log.find()
{ “_id” : ObjectId(“5e9d2f4eee592e24d2cdc2c7”), “account” : “test” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc98”), “account” : “test1” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc99”), “account” : “test2” }
再來如果只是想修改某一個值 舉例 我要修改將 account test 改成 account test3
修改指令為
db.op_log.update({"account":"test"},{$set:{"account":"test3"}})
說明 {“account”:”test”} 為query 就類似 where 條件 ,{$set:{“account”:”test3″}} 要修改的值
這邊要注意這指令只會變更第一筆資料,如果要同時更新面同的條件資料需多加 {multi:true} 指令如下
db.op_log.update({"account":"test"},{$set:{"account":"test3"}},{multi:true})
查詢 db.op_log.find()
{ “_id” : ObjectId(“5e9d2f4eee592e24d2cdc2c7”), “account” : “test3” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc98”), “account” : “test1” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc99”), “account” : “test2” }
刪除
db.op_log.remove({"_id":ObjectId("5e9d2f4eee592e24d2cdc2c7")})
成功會顯示
WriteResult({ “nRemoved” : 1 })
查詢 db.op_log.find()
{ “_id” : ObjectId(“5e9d38579afa78b4837efc98”), “account” : “test1” }
{ “_id” : ObjectId(“5e9d38579afa78b4837efc99”), “account” : “test2” }
刪除符合條件的多筆資料
db.op_log.remove({"account":"test4"})
成功會顯示
WriteResult({ “nRemoved” : 2 })
官方建議的刪除方式
刪除單筆資料
db.op_log.deleteOne({"account":"test4"})
成功會顯示
{ “acknowledged” : true, “deletedCount” : 1 }
刪除多筆資料
db.op_log.deleteMany({"account":"test4"})
成功會顯示
{ “acknowledged” : true, “deletedCount” : 3 }
刪除所有資料
db.op_log.deleteMany({})
成功會顯示
{ “acknowledged” : true, “deletedCount” : 6 }
香積麵 藥膳 風味 84gx5包 素泡麵 純素 素食 蔬食 慈濟 靜思
3M 日本原裝進口 速效型 防水噴霧 170ml 衣物 鞋子 皮革 透氣 防水 團購
Xilla 露營 戶外 大力夾雲台手機支架 拓展支架 球型雲台夾 手機架
維力 素飄香 野菜多 素排骨雞 素紅燒牛肉 麻辣燙 麻油當歸枸杞 蔘湯麵 素食 泡麵 單包