Mongo – 后台紧凑集合

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月3日 04:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定