如何在 MongoDB shell 中中止正在运行的查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26940616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:12:33  来源:igfitidea点击:

How do I abort a running query in the MongoDB shell?

mongodbmongodb-query

提问by collisionTwo

I can't believe I have to ask this, but how do I stop a query I just ran, which is now running, and will obviously take a very long time to complete in the Mongo shell? Control+Cappears to crash the shell, and spits out a ton of errors. The silly solutions suggested in this postof course do not do anything. I understand that I could like open up another terminal tab and run db.currentOp(), find the operation ID, and then run db.killOp(), but I can't believe that's the only solution. I must be missing something obvious.

我不敢相信我不得不问这个问题,但是如何停止我刚刚运行的查询,该查询现在正在运行,并且显然需要很长时间才能在 Mongo shell 中完成?Control+C似乎使外壳崩溃,并吐出大量错误。这篇文章中建议的愚蠢解决方案当然没有任何作用。我知道我可以打开另一个终端选项卡并运行db.currentOp(),找到操作 ID,然后运行db.killOp(),但我不敢相信这是唯一的解决方案。我一定遗漏了一些明显的东西。

回答by Roozbeh Zabihollahi

Based on Alex's answer.

基于亚历克斯的回答。

  1. Query current running operations:

    db.currentOp()
    
  1. 查询当前运行的操作:

    db.currentOp()
    

Mongo currentOp Response

Mongo currentOp 响应

  1. Kill the operation based on opid

    db.killOp(30318806)
    
  1. 杀死基于opid的操作

    db.killOp(30318806)
    

回答by Alex Erygin

According to Mongo documentation, you should:

根据 Mongo文档,您应该:

  1. Obtain the current operation ID via db.currentOp()
  2. Kill opp via db.killOp()
  1. 通过获取当前操作ID db.currentOp()
  2. 杀死 opp 通过 db.killOp()

Good example script can be found here.

好的示例脚本可以在这里找到。

回答by Yuriy Bosov

For sharded cluster:

对于分片集群:

  1. db.currentOp()
  2. db.killOp("shard_id:opid")
  1. db.currentOp()
  2. db.killOp("shard_id:opid")

For example:

例如:

db.killOp("shard0000:3134616")