How to import and export MongoDB indices to JSON with PyMongo

1.3k views Asked by At

I've got a situation where I need to maintain identical indices on MongoDB databases between environments, but I can't use mongodump/restore because I don't want to copy the data, I just import and export the indices as JSON docs. I need a handy way, preferably using Pymongo, to export the indices for all collections in a given Db (or all DBs on the instance) into a single JSON file, then be able to take that file and apply the indices to another instance to ensure all the DB names, collection names, and indices on those collections are identical between environments (I'd also like to be able to maintain collection cap limits if they exist as well).

Does anyone have a handy code example that does this? Would you use db.collection.index_information() and if so, how would you encode the output into a JSON doc that could be later read by the same script and imported into another instance?

0

There are 0 answers