英文:
Mongo - compact collection in background
问题
I want to compact very large collection in mongo (around 800GB of data).
I know that I need to run it on separate replicas (first secondaries, then promote another node to primary, and then compact last secondary). And that is fine.
to do this i need to run:
db.runCommand({compact: "collectionName"})
But i want to execute it in background. And this is my problem because when I execute:
mongosh mongodb://database.host.net:27017/databaseName --eval "db.runCommand({compact: "collectionName"})"
I'm getting message:
ReferenceError: collectionName is not defined
How can i run compacting in background ?
英文:
I want to compact very large collection in mongo (around 800GB of data).
I know that I need to run it on separate replicas (first secondaries, then promote another node to primary, and then compact last secondary). And that is fine.
to do this i need to run:
db.runCommand({compact: "collectionName"})
But i want to execute it in background. And this is my problem because when I execute:
mongosh mongodb://database.host.net:27017/databaseName --eval "db.runCommand({compact: "collectionName"})"
I'm getting message:
ReferenceError: collectionName is not defined
How can i run compacting in background ?
答案1
得分: 1
错误消息 ReferenceError: collectionName 未定义
是由于您的命令格式不正确造成的。您需要在嵌套引号周围使用不同类型的引号,否则将会导致像您那样的语法错误。
尝试使用以下方式:
mongosh mongodb://database.host.net:27017/databaseName --eval ''db.runCommand({compact: "collectionName"})''
注意我在 --eval
周围使用了单引号 '
而不是双引号 "
。
要在后台运行它,使用这个命令:
nohup mongosh mongodb://database.host.net:27017/databaseName --eval ''db.runCommand({compact: "collectionName"})'' &
&
使命令在后台运行。而 nohup
使命令在终端关闭时仍然运行。
英文:
The message ReferenceError: collectionName is not defined
you're getting is due to how your command is formatted. You need to use different quotes for embedded quotes, otherwise it will cause syntax errors like yours.
Try instead of:
mongosh mongodb://database.host.net:27017/databaseName --eval "db.runCommand({compact: "collectionName"})"
A command:
mongosh mongodb://database.host.net:27017/databaseName --eval 'db.runCommand({compact: "collectionName"})'
Note how I used single quotes '
instead of double quotes "
around --eval
.
And to run it on background, use this:
nohup mongosh mongodb://database.host.net:27017/databaseName --eval 'db.runCommand({compact: "collectionName"})' &
&
makes a command run on background. And nohup
makes a command run even when terminal is closed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论