Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB import/export indexes

Tags:

I need a tool for rapidly recreating the proper "schema" (such as it is) of MongoDB instances between environments that pre-creates the right DB names, collections, sets collection caps, and creates the indexes for each collection. I do NOT want to copy all of the data between the instances, though. Each env I manage has different data but the DB/collection/caps/indexes are all the same. Is there an easy way to do this, preferably a tool that exports a JSON doc of all the names, caps and indexes that can then be re-imported into a new instance?

like image 441
perdurabo93 Avatar asked Jun 29 '15 15:06

perdurabo93


People also ask

Does Mongodump dump indexes?

Yes, mongodump does export the indexes created on the collection, and the indexes are restored with mongorestore along with the data.

What is the difference between Mongodump and Mongoexport?

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.


1 Answers

After I didn't find any appropriate tool, I dumped the database and restored it without data, so metadata is restored (particularly all the collections are created with apropriate indexes), but the collections are empty. This also can be applied to databases with data as well, so the indexes will be created on existing db if there is no conflict.

1.Make a dump with mongodump

2.Empty the data of collections

find ./dump -name '*bson' -type f -exec cp /dev/null {} \;

3.Restore the data to new db with mongorestore

like image 133
Menua Avatar answered Sep 22 '22 16:09

Menua