Skip to content

Instantly share code, notes, and snippets.

@luckydev
Created May 26, 2021 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckydev/f9d28af6ff390d57d582799d2dd73f06 to your computer and use it in GitHub Desktop.
Save luckydev/f9d28af6ff390d57d582799d2dd73f06 to your computer and use it in GitHub Desktop.
Create MongoDB index in background
//query to create an index in background. won't stop current reads/writes on the collection
db.collection.createIndex( { 'key1': 1 }, { background: true } )
//query to create a compount index in background. won't stop current reads/writes on the collection
db.collection.createIndex( { 'key1': 1, 'key2': 1 }, { background: true } )
//to check the progress of indexing operation
db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.msg) })
//to kill the indexing operation, just in case.
db.killOp(<opid of the query to kill>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment