Taking backup of a small subset of documents in a collection
In the previous recipe, we saw how to backup certain databases and their collections. In this recipe, we will look at how to use the mongodump
utility to backup specific documents within a collection.
Getting ready
You need a single node MongoDB installation, preferably with some data in it. Refer to the recipe Creating an index in Chapter 2, Understanding and Managing Indexes, on how to import sample data into a MongoDB instance.
How to do it...
- Open the mongo shell and check all documents for a specific language:
use mydb db.mockdata.count({language:"Thai"})
- You should see the following output:
892
- Add the following to
query.json
:
{language: 'Thai'}
- Execute the
mongodump
utility with a specific query:
mongodump -d mydb -c mockdata --queryFile query.json
- You should see the following output:
2017-10-04T12:17:28.559+0000 writing mydb.mockdata 2017-10-04T12:17:28.596+0000 done dumping mydb.mockdata (892 documents)
How it works...
By now, these...