skip to content
Alvin Lucillo

Check running index builds

/ 4 min read

You can check any ongoing index builds by using the aggregation stage $currentOp. In the sample code below, 500,000 documents are inserted first into the collection, and multiple indexes are created. Due to the number of documents and indexes, the index build might take time, that’s why I was able to capture the operation.

The important part of the result is the one with desc IndexBuildsCoordinatorMongod-2 than the one with conn20. In that document, we can see:

  1. the msg value states what it’s doing: Index Build: inserting keys from external sorter into index

  2. the createIndexes command for big collection and the list of indexes

  3. it has acquired locks (locks). Based on the docs (see reference), Database: "w" means it has intent exclusive lock on the database

  4. it is not waiting for any locks to be released first (waitingForLock: false)

use repro_currentop

db.big.drop()

const bulk = []
for (let i = 0; i < 500_000; i++) {
  bulk.push({
    userid: "user-" + i.toString().padStart(6, "0"),
    email: "user-" + i.toString().padStart(6, "0") + "@example.com",
    accountId: i % 1000,
    region: "r-" + (i % 50),
    status: ["new", "open", "closed"][i % 3],
    createdAt: new Date(),
    score: i % 10000
  })

  if (bulk.length === 1000) {
    db.big.insertMany(bulk)
    bulk.length = 0
  }
}

if (bulk.length) db.big.insertMany(bulk)

db.runCommand({
  createIndexes: "big",
  indexes: [
    { key: { userid: 1 }, name: "userid_1" },
    { key: { email: 1 }, name: "email_1" },
    { key: { accountId: 1, userid: 1 }, name: "accountId_1_userid_1" },
    { key: { region: 1, status: 1, createdAt: 1 }, name: "region_1_status_1_createdAt_1" },
    { key: { score: 1, createdAt: -1 }, name: "score_1_createdAt_-1" }
  ]
})
use admin

while (true) {
  const ops = db.aggregate([
    { $currentOp: { allUsers: true, idleConnections: false } },
    {
      $match: {
        "command.createIndexes": "big",
        "command.$db": "repro_currentop"
      }
    }
  ]).toArray()

  printjson(ops)
  sleep(1000)
}
[
	{
		type: "op",
		host: "ea3f589b0ffc:27017",
		desc: "conn20",
		connectionId: 20,
		client: "172.18.0.1:45602",
		appName: "mongosh 2.3.8",
		clientMetadata: {
			application: {
				name: "mongosh 2.3.8",
			},
			driver: {
				name: "nodejs|mongosh",
				version: "6.12.0|2.3.8",
			},
			platform: "Node.js v20.18.1, LE",
			os: {
				name: "linux",
				architecture: "x64",
				version: "3.10.0-327.22.2.el7.x86_64",
				type: "Linux",
			},
		},
		active: true,
		currentOpTime: "2026-05-02T07:32:47.469+00:00",
		threaded: true,
		opid: 198319,
		lsid: {
			id: UUID("ce97dbf6-019e-45df-8af4-bd4494a25d5f"),
			uid: Binary.createFromBase64("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", 0),
		},
		secs_running: Long("1"),
		microsecs_running: Long("1395055"),
		op: "command",
		ns: "repro_currentop.big",
		redacted: false,
		command: {
			createIndexes: "big",
			indexes: [
				{
					key: {
						userid: 1,
					},
					name: "userid_1",
				},
				{
					key: {
						email: 1,
					},
					name: "email_1",
				},
				{
					key: {
						accountId: 1,
						userid: 1,
					},
					name: "accountId_1_userid_1",
				},
				{
					key: {
						region: 1,
						status: 1,
						createdAt: 1,
					},
					name: "region_1_status_1_createdAt_1",
				},
				{
					key: {
						score: 1,
						createdAt: -1,
					},
					name: "score_1_createdAt_-1",
				},
			],
			lsid: {
				id: UUID("ce97dbf6-019e-45df-8af4-bd4494a25d5f"),
			},
			$db: "repro_currentop",
		},
		numYields: 0,
		locks: {},
		waitingForLock: false,
		lockStats: {
			ParallelBatchWriterMode: {
				acquireCount: {
					r: Long("4"),
				},
			},
			FeatureCompatibilityVersion: {
				acquireCount: {
					r: Long("1"),
					w: Long("4"),
				},
			},
			ReplicationStateTransition: {
				acquireCount: {
					w: Long("5"),
				},
			},
			Global: {
				acquireCount: {
					r: Long("1"),
					w: Long("4"),
				},
			},
			Database: {
				acquireCount: {
					r: Long("1"),
					w: Long("3"),
				},
			},
			Collection: {
				acquireCount: {
					r: Long("1"),
					w: Long("2"),
					W: Long("1"),
				},
			},
			Mutex: {
				acquireCount: {
					r: Long("1"),
				},
			},
		},
		waitingForFlowControl: false,
		flowControlStats: {
			acquireCount: Long("3"),
		},
	},
	{
		type: "op",
		host: "ea3f589b0ffc:27017",
		desc: "IndexBuildsCoordinatorMongod-2",
		active: true,
		currentOpTime: "2026-05-02T07:32:47.469+00:00",
		opid: 198320,
		secs_running: Long("1"),
		microsecs_running: Long("1394488"),
		op: "command",
		ns: "repro_currentop.big",
		redacted: false,
		command: {
			createIndexes: "big",
			indexes: [
				{
					v: 2,
					key: {
						userid: 1,
					},
					name: "userid_1",
				},
				{
					v: 2,
					key: {
						email: 1,
					},
					name: "email_1",
				},
				{
					v: 2,
					key: {
						accountId: 1,
						userid: 1,
					},
					name: "accountId_1_userid_1",
				},
				{
					v: 2,
					key: {
						region: 1,
						status: 1,
						createdAt: 1,
					},
					name: "region_1_status_1_createdAt_1",
				},
				{
					v: 2,
					key: {
						score: 1,
						createdAt: -1,
					},
					name: "score_1_createdAt_-1",
				},
			],
			lsid: {
				id: UUID("ce97dbf6-019e-45df-8af4-bd4494a25d5f"),
			},
			$db: "repro_currentop",
		},
		msg: "Index Build: inserting keys from external sorter into index",
		numYields: 1500,
		locks: {
			FeatureCompatibilityVersion: "w",
			ReplicationStateTransition: "w",
			Global: "w",
			Database: "w",
			Collection: "w",
		},
		waitingForLock: false,
		lockStats: {
			FeatureCompatibilityVersion: {
				acquireCount: {
					w: Long("1502"),
				},
			},
			ReplicationStateTransition: {
				acquireCount: {
					w: Long("1502"),
				},
			},
			Global: {
				acquireCount: {
					w: Long("1502"),
				},
			},
			Database: {
				acquireCount: {
					w: Long("1502"),
				},
			},
			Collection: {
				acquireCount: {
					w: Long("1501"),
					W: Long("1"),
				},
			},
		},
		waitingForFlowControl: false,
		flowControlStats: {
			acquireCount: Long("1502"),
		},
	},
];

Reference:

  1. https://www.mongodb.com/docs/manual/reference/command/currentOp/