63

I'm using MongoDB and have a collection with roughly 75 million records. I have added a compound index on two "fields" by using the following command:

db.my_collection.ensureIndex({"data.items.text":1, "created_at":1},{background:true}).

Two days later I'm trying to see the status of the index creation. Running db.currentOp() returns {}, however when I try to create another index I get this error message:

cannot add index with a background operation in progress.

Is there a way to check the status/progress of the index creation job?

One thing to add - I am using mongodb version 2.0.6. Thanks!

Yair Avgar
  • 631
  • 1
  • 5
  • 5
  • 1
    What does db.my_collection.getIndexes() show you? Is the index already created? – Anand Jayabalan Mar 10 '14 at 19:22
  • This is also a good link: http://docs.mongodb.org/manual/tutorial/manage-in-progress-indexing-operations/ – Sammaye Mar 10 '14 at 19:40
  • 2
    Anand - getIndexes() lists the new index. However, I'm not sure that it means that the job completed. – Yair Avgar Mar 10 '14 at 23:43
  • By what I've just experienced and read in here if an Index appears under the Collection it still doesn't mean the job is completed. Am I correct? – Guy_g23 Jul 06 '23 at 13:38

7 Answers7

72

At the mongo shell, type below command to see the current progress:

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%

To do a real-time running status log:

> while (true) { db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.msg) }); sleep(1000); }
Index Build: scanning collection Index Build: scanning collection: 43687948/47760207 91%
Index Build: scanning collection Index Build: scanning collection: 43861991/47760228 91%
Index Build: scanning collection Index Build: scanning collection: 44993874/47760246 94%
Index Build: scanning collection Index Build: scanning collection: 45968152/47760259 96%
Claudiu
  • 224,032
  • 165
  • 485
  • 680
Bajal
  • 5,487
  • 3
  • 20
  • 25
  • 1
    I typed this in my mongo shell but nothing comes out. And if I just typed db.currentOp() it will output "inprog" : [ { "host" : "ip-172-31-18-127:27017", "desc" : "conn28", "connectionId" : 28, "client" : "127.0.0.1:34690", "appName" : "MongoDB Shell", "clientMetadata" : { "application" : { "name" : "MongoDB Shell" }, – imin Apr 05 '19 at 04:17
  • 1
    For sharded cluster: db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.shard, op.msg) }) – Martin P. Apr 24 '20 at 18:37
  • I just see "Index Build: draining writes received during build" - any ideas? – Hackeron Jun 13 '22 at 14:08
  • This also works with Azure Cosmos DB for MongoDB – Abu Sufian Jan 31 '23 at 02:18
15

You could use currentOp with a true argument which returns a more verbose output, including idle connections and system operations.

db.currentOp(true)

... and then you could use db.killOp() to Kill the desired operation.

CesarTrigo
  • 423
  • 1
  • 3
  • 10
  • Thank you Cesar - running db.currentOp(true) returns the same {}. Any other ideas? – Yair Avgar Mar 10 '14 at 23:44
  • 3
    Thank you all for the suggestions. After a lot more searching I found [this post](http://blog.mongolab.com/2014/02/mongodb-currentop-killop/)! that clarifies that in an auth-enabled environment db.currentOp() works only for admin users, which are users that auth into the admin db. Once I used that user I saw real output from the command. – Yair Avgar Mar 12 '14 at 14:56
14

The following should print out index progress:

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

outputs:

Index Build (background) Index Build (background): 5311727/27231147 19%
DR9885
  • 161
  • 1
  • 4
  • 1
    That didn't work for me (prints nothing), so I modified it to `db.currentOp(true).inprog.forEach(function(op){ print(op.msg) })` and now it prints additional rows of `[unknown type]` stuff but at least it prints the index build % as well, which is a lot cleaner than CesarTrigo's `db.currentOp(true)` that print's a lot more stuff – Computer's Guy Apr 30 '18 at 20:33
  • Use this instead: `db.currentOp({ "query.createIndexes": { $exists : true } }).inprog.forEach(function(op){ print(op.msg) })` – Corvin Feb 07 '19 at 21:31
14

Unfortunately, DR9885 answer didn't work for me, it has spaces in the code (syntax error) and even if the spaces are removed, it returns nothing.

This works as of Mongo Shell v3.6.0

db.currentOp().inprog.forEach(function(op){ if(op.msg) print(op.msg) })

Didn't read Bajal answer until after I posted mine, but it's almost exactly the same except that it's slightly shorter code and also works.

Computer's Guy
  • 5,122
  • 8
  • 54
  • 74
  • I typed this in my mongo shell but nothing comes out. And if I just typed `db.currentOp()` it will output `"inprog" : [ { "host" : "ip-172-31-18-127:27017", "desc" : "conn28", "connectionId" : 28, "client" : "127.0.0.1:34690", "appName" : "MongoDB Shell", "clientMetadata" : { "application" : { "name" : "MongoDB Shell" },` – imin Apr 05 '19 at 04:19
6

I like:

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

Output example:

Index Build Index Build: 84826/335739 25%

Documentation suggests:

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

Active Indexing Operations example.

FPC
  • 3,422
  • 1
  • 16
  • 12
3

Simple one to just check progress of a single index going on:

db.currentOp({"msg":/Index/}).inprog[0].progress;

outputs:

{ "done" : 86007212, "total" : 96868386 }
Ian Marett
  • 31
  • 2
1

Find progress of index jobs, nice one liner:

> db.currentOp().inprog.map(a => a.msg)
[
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    "Index Build: scanning collection Index Build: scanning collection: 16448156/54469342 30%",
    undefined,
    undefined
]
Phil Poore
  • 2,086
  • 19
  • 30