linux平台使用脚本删除mongodb数据库中文档并释放存储空间

需求描述:需要删除mongodb数据库中无效文档,并且释放存储空间。

一、删除文档

在终端中运行脚本如下:

[root@dytech dy]# mongo ppl -u "root" -p "dengyu666" delDoc.txt
root 是数据库用户名 ,必须要用英文引号;
dengyu666 是数据库密码,必须要用英文引号;
每个单词之间要有一个英文空格。

其中delDoc.txt要在dy目录下 ,也可以写delDoc.txt的绝对路径。名字随意,尽量不要用中文及特殊字符。

delDoc.txt的内容如下:

db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb06aecf5d1ef06d2170")});
db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb06aecf5d272031e2bc")});
db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb06aecf5d26d454bdd8")});
db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb06aecf5d1ba8b22eb0")});
db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb05aecf5d0c74cd0235")});
db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb05aecf5d1ef06d216f")});
db.report_info_x_nurse.deleteMany({"_id":ObjectId("644cfb05aecf5d272031e2bb")});

delDoc.txt内容是用代码或者其他方式,根据需要删除的文档的objectId生成的。deleteMany也可以用remove。

办法比较笨,欢迎高手指点更高效、安全的方法。

二、释放空间

依次执行下方“PRIMARY> ”后的语句(用户、密码、数据库、集合名称需结合实际修改)


终端运行命令:
1、调出mongodb命令行:
[root@dytech ~]# mongo

MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName                                                               =mongodb
Implicit session: session { "id" : UUID("a5cf6810-9810-466c-9cb1-2d94ede58469")                                                                }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been d                                                               eprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
2、切换到admin数据库以便完成用户、密码认证登录
dycc:PRIMARY> use admin
switched to db admin
3、用户、密码登录
dycc:PRIMARY> db.auth('root','haitaiinc')
1
4、切换到需要压缩的集合所在的数据库
dycc:PRIMARY> use ppl
switched to db ppl
5、运行压缩命令
dycc:PRIMARY> db.runCommand({compact:"report_info_x_us"})
{
        "ok" : 0,
        "errmsg" : "will not run compact on an active replica set primary as thi                                                               s is a slow blocking operation. use force:true to force",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1684921863, 3),
                "signature" : {
                        "hash" : BinData(0,"6IcwSwUHbVQ8FOBv1ebhyLZqbls="),
                        "keyId" : NumberLong("7178501260704743430")
                }
        },
        "operationTime" : Timestamp(1684921863, 3)
}
上面运行报错,提示如下:
"errmsg" : "will not run compact on an active replica set primary as this is a slow blocking operation. use force:true to force",
所以加上force参数再次运行:
db.runCommand({compact:"report_info_x_us",force:true})
返回消息:
{
        "bytesFreed" : 0,
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1684922672, 3),
                "signature" : {
                        "hash" : BinData(0,"MDJTOh1/5txj0z4aO+lmruiiwDo="),
                        "keyId" : NumberLong("7178501260704743430")
                }
        },
        "operationTime" : Timestamp(1684922672, 3)
}
可见消息中有如下内容:
      "ok" : 1,
则空间释放完成。这时候再查看磁盘空间会看到可用空间有变动。
      

你可能感兴趣的:(数据库,mongodb,数据库,mongodb,linux)