Say you liked the indexes you created on system A and wanted them on system B.

Say you are a horrible typist.

Say you are reading this blog, and are good at COPY+PASTE.


SET HOST=eqcmongo01.eq.colo/

SET DB=Photologica

mongo %HOSt%%DB% --quiet --eval "db.system.indexes.find({'key._id':{$exists:false}}).forEach(function(c){ print(c.ns.replace('%DB%','db') + '.ensureIndex('); printjson(c.key); print( ');') ;})"


The recipe is:

  • Run mongo shell in quiet mode.
  • Query out the indexes except the "natural" _id ones (although not a problem to script out, this is noise)
  • Output an ensureIndex() command for each index.
  • Run the batch file, redirect output into a indexExport.js file.
  • Run mongo [target server] indexExport.js

Well, this is kind of cool. You might be thinking there's an in-shell way of doing the same. You are correct.

One reason to create a file artifact is that you can source-control it and repeat the process when bringing up new environments.