Mongodump multiple collections

Hi Team,

Can we take mongodump of more than 1 collection in a single command?

Thanks!

According to the documentation https://docs.mongodb.com/database-tools/mongodump/ there is no explicit options for that. However, if you do not specify a collection, then all collections of the given database are dumped. You may exclude many collections by specifying the option –excludeCollection multiple times. May be using multiple –collection options could work like multiple –excludeCollection. It is worth the try. Share the results. Also take a look at the option –excludeCollectionsWithPrefix.

Hi Steeve,

Yes, I checked that still thought to double check. I think mongo team should bring this feature soon. Will be useful.

Thanks,
Rajeev

Hi @Rajeev_Vishal,

Can you explain your use case in a bit more detail? As @steevej noted you can use --excludeCollection or --excludeCollectionsWithPrefix multiple times.

There’s an open feature request to add similar options for including collections that you can watch or upvote: TOOLS-1731.

As an alternative approach, you can write a shell script to dump a selection of collections.

A very simplified example to mongodump a list of collections from the test database in a local mongod instance:

#!/bin/bash

# Array of collection names
collections=(foo bar baz)

for collection in $collections; do
  	mongodump --db test --collection $collection
done

Regards,
Stennie

1 Like

A use case is I have 170 collections and would like to dump 34 of them.

1 Like