mongodb索引创建工作的状态

Yai*_*gar 27 indexing jobs background mongodb

我正在使用MongoDB并拥有大约7500万条记录的集合.我使用以下命令在两个"字段"上添加了复合索引:

db.my_collection.ensureIndex({"data.items.text":1, "created_at":1},{background:true}).
Run Code Online (Sandbox Code Playgroud)

两天后,我试图看到索引创建的状态.运行db.currentOp()返回{},但是当我尝试创建另一个索引时,我收到此错误消息:

cannot add index with a background operation in progress.
Run Code Online (Sandbox Code Playgroud)

有没有办法检查索引创建作业的状态/进度?

有一点要补充 - 我使用的是mongodb 2.0.6版.谢谢!

Baj*_*jal 27

在mongo shell中,键入以下命令以显示当前进度:

rs0:PRIMARY> db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.msg) })

Index Build (background) Index Build (background): 1431577/55212209 2%
Run Code Online (Sandbox Code Playgroud)


Ces*_*igo 12

您可以将currentOptrue参数一起使用,该参数返回更详细的输出,包括空闲连接和系统操作.

db.currentOp(true)
Run Code Online (Sandbox Code Playgroud)

...然后你可以使用db.killOp()来杀死所需的操作.

  • 谢谢大家的建议。经过大量搜索,我找到了 [这篇文章](http://blog.mongolab.com/2014/02/mongodb-currentop-killop/)!这澄清了在启用身份验证的环境中 db.currentOp() 仅适用于管理员用户,这些用户是授权到管理员数据库的用户。使用该用户后,我看到了命令的真实输出。 (2认同)

小智 7

以下应打印出索引进度:

db
  .currentOp({"command.createIndexes": { $exists : true } })
  .inprog
  .forEach(function(op){ print(op.msg) })
Run Code Online (Sandbox Code Playgroud)

输出:

Index Build (background) Index Build (background): 5311727/27231147 19%
Run Code Online (Sandbox Code Playgroud)


oct*_*ron 7

不幸的是,DR9885的答案对我不起作用,他在代码中张贴了空格(语法错误),即使空格被删除,它也不返回任何内容。

从Mongo Shell开始使用 v3.6.0

db.currentOp().inprog.forEach(function(op){ if(op.msg) print(op.msg) })
Run Code Online (Sandbox Code Playgroud)

在我发布我的文章之前,我没有读过Bajal的答案,但是它几乎完全一样,只是它的代码略短,并且也可以使用。


FPC*_*FPC 5

我喜欢:

db.currentOp({ 
    'msg' :{ $exists: true },
    'command': { $exists: true },
    $or: [ 
        { 'command.createIndexes': { $exists: true } }, 
        { 'command.reIndex': { $exists: true } }
    ]
}).inprog.forEach(function(op) { 
    print(op.msg); 
});
Run Code Online (Sandbox Code Playgroud)

输出示例:

索引构建 索引构建:84826/335739 25%

文档建议:

db.adminCommand(
    {
      currentOp: true,
      $or: [
        { op: "command", "command.createIndexes": { $exists: true }  },
        { op: "none", "msg" : /^Index Build/ }
      ]
    }
)
Run Code Online (Sandbox Code Playgroud)

活动索引操作示例