supreme0100
supreme0100

Reputation: 99

How to export subset of data from mongodb

I currently have a large database, and I need a means of backing up subsets of the data that can then be imported on another mongodb instance.

For example, I would need to find all documents that contain a key, so essentially: find({key: 'somekey'}), and then export that data set. I thought to simply run the query in NodeJS, and save the data in JSON format. I don't think this is optimal as through my understanding simply importing the JSON data again (if needed in the future) won't be a straightforward task as the data-types will be lost.

So my question is, how would I go about exporting a subset of the dataset so that it may be possibly re-imported into another mongodb instance on another server.

Upvotes: 2

Views: 1798

Answers (1)

supreme0100
supreme0100

Reputation: 99

Thanks to @Veeram's comment, the way to do this is as BSON so that it retains all the data structure:

sudo mongodump -d DB_Name -c Collection -q '{"key_name": "value"}' --out /home/collection

Then to import it back:

sudo mongorestore -d DB_Name -c Collection  /home/collection/DB_Name/Collection.bson

Upvotes: 4

Related Questions