MongoDB數(shù)據(jù)庫與集合刪除操作方法詳解
1. MongoDB 數(shù)據(jù)刪除架構(gòu)總覽

2. 數(shù)據(jù)庫刪除詳解
2.1 刪除數(shù)據(jù)庫流程圖

2.2 數(shù)據(jù)庫刪除步驟
確認(rèn)當(dāng)前數(shù)據(jù)庫:
db // 顯示當(dāng)前數(shù)據(jù)庫
切換到目標(biāo)數(shù)據(jù)庫:
use db_to_delete
執(zhí)行刪除命令:
db.dropDatabase()
驗證刪除結(jié)果:
show dbs
2.3 刪除示例
// 查看所有數(shù)據(jù)庫
> show dbs
admin 240.00 KiB
config 108.00 KiB
local 80.00 KiB
mytest_db 16.00 KiB
// 切換到目標(biāo)數(shù)據(jù)庫
> use mytest_db
switched to db mytest_db
// 刪除數(shù)據(jù)庫
> db.dropDatabase()
{ "dropped" : "mytest_db", "ok" : 1 }
// 驗證刪除
> show dbs
admin 240.00 KiB
config 108.00 KiB
local 80.00 KiB
3. 集合刪除詳解
3.1 集合刪除流程圖

3.2 集合刪除步驟
- 查看數(shù)據(jù)庫中的集合:
show collections

執(zhí)行刪除命令:
db.collection_name.drop()
驗證刪除結(jié)果:
show collections
3.3 刪除示例
// 創(chuàng)建測試集合
> db.createCollection("products")
{ "ok" : 1 }
// 查看集合
> show collections
products
// 刪除集合
> db.products.drop("products")
true
// 驗證刪除
> show collections
>

4. 刪除操作對比
| 特性 | dropDatabase | drop |
|---|---|---|
| 作用范圍 | 整個數(shù)據(jù)庫 | 單個集合 |
| 刪除內(nèi)容 | 所有集合+元數(shù)據(jù) | 文檔+索引 |
| 執(zhí)行速度 | 較慢 | 較快 |
| 鎖級別 | 數(shù)據(jù)庫鎖 | 集合鎖 |
| 返回結(jié)果 | {dropped: “db_name”, ok: 1} | true/false |
| 恢復(fù)可能 | 需要備份 | 需要備份 |
5. 刪除前的安全檢查

安全檢查清單:
- 確認(rèn)數(shù)據(jù)庫/集合名稱
- 檢查應(yīng)用程序依賴
- 驗證用戶權(quán)限
db.runCommand({connectionStatus: 1}).authInfo.authenticatedUserPrivileges - 確保有最新備份
6. 高級刪除場景
6.1 條件刪除文檔
// 刪除所有匹配文檔
db.orders.deleteMany({status: "cancelled"})
// 刪除單個匹配文檔
db.orders.deleteOne({_id: ObjectId("...")})
6.2 刪除系統(tǒng)集合
// 刪除system.profile集合(需在admin庫) use admin db.setProfilingLevel(0) db.system.profile.drop()
6.3 使用mongodump備份后刪除
# 先備份 mongodump --db sales --out /backup/ # 再刪除 mongosh --eval "use sales; db.dropDatabase()"
7. 刪除操作監(jiān)控
7.1 查看刪除操作
// 查看當(dāng)前操作
db.currentOp({
"op": "command",
"command.drop": {$exists: true}
})
7.2 審計日志配置
# mongod.conf
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/audit.json
filter: '{ atype: "dropCollection" }'
8. 故障恢復(fù)方案
8.1 誤刪恢復(fù)流程

8.2 常用恢復(fù)命令
# 從備份恢復(fù)整個數(shù)據(jù)庫 mongorestore --db sales /backup/sales/ # 恢復(fù)特定集合 mongorestore --db sales --collection orders /backup/sales/orders.bson
9. 最佳實踐建議
刪除前雙重確認(rèn):
// 確認(rèn)數(shù)據(jù)庫大小 db.stats() // 確認(rèn)集合文檔數(shù) db.products.countDocuments()
生產(chǎn)環(huán)境防護:
// 禁止危險操作(開發(fā)環(huán)境) db.adminCommand({ setParameter: 1, disableDropDatabase: 1 })自動化備份策略:
# 每日定時備份 0 2 * * * mongodump --out /backup/$(date +\%Y\%m\%d)
權(quán)限控制:
// 創(chuàng)建專用管理用戶 use admin db.createUser({ user: "dbadmin", pwd: "securepassword", roles: ["dbAdminAnyDatabase"] })
通過本文的全面介紹,您應(yīng)該已經(jīng)掌握了MongoDB中數(shù)據(jù)庫和集合刪除的所有關(guān)鍵知識。請始終記?。簞h除操作是不可逆的,執(zhí)行前務(wù)必備份重要數(shù)據(jù)并確認(rèn)操作對象!

總結(jié)
到此這篇關(guān)于MongoDB數(shù)據(jù)庫與集合刪除操作方法的文章就介紹到這了,更多相關(guān)MongoDB數(shù)據(jù)庫與集合刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB 內(nèi)存管理相關(guān)總結(jié)
這篇文章主要介紹了MongoDB 內(nèi)存管理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用MongoDB數(shù)據(jù)庫,感興趣的朋友可以了解下2021-03-03
MongoDB 中聚合統(tǒng)計計算--$SUM表達式
這篇文章主要介紹了MongoDB 中聚合統(tǒng)計計算--$SUM表達式的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
Linux服務(wù)器下MariaDB 10自動化安裝部署
這篇文章主要介紹了Linux服務(wù)器下MariaDB 10自動化安裝部署,需要的朋友可以參考下2016-08-08
windows安裝mongodb6.x并設(shè)置用戶名密碼的詳細過程
這篇文章主要介紹了windows安裝mongodb6.x并設(shè)置用戶名密碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
Mongodb啟動報錯完美解決方案:about to fork child pr
在使用命令行啟動 MongoDB 的時候報錯:about to fork child process, waiting until server is ready for connections.forked process: 50411,造成這個報錯的原因是 “MongoDB” 服務(wù)沒有正常的關(guān)閉,在終端連接非正常斷開后,再次執(zhí)行 MongoDB 的時候報錯2023-04-04

