-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
1. shell 清空所有 collection 数据但保留 collection 及索引
#!/bin/bash
DB_NAME="your_db_name"
DB_HOST="your_db_host"
DB_USER="your_db_username"
DB_PASS="your_db_password"
MONGODB_URI="mongodb+srv://$DB_USER:$DB_PASS@$DB_HOST/$DB_NAME"
COLLECTIONS=$(mongosh $MONGODB_URI --quiet --eval "show collections")
mongosh $MONGODB_URI --eval "use stamsel; db.getCollectionNames().forEach((col) => {
db[col].deleteMany({});
});"保存以上代码到 cleanup.sh 中,并使用指令 chmod +x cleanup.sh 为文件添加可执行权限。
执行 ./cleanup.sh 即可,cleanup.sh 是 sh 的文件名,可以自己随意起一个。
PS:
此处用的 mongosh, 也可以用 mongo 替代, 二者都是 MongoDB 官方出品。mongosh 比 mongo 支持的功能更多