DalhousieDuck
DalhousieDuck

Reputation: 339

When using the Mongodump shell, how do you specify the database and collection from a replica set?

I need to create a mongodump of a specific collection from a specific database in a replica set. I am issuing the following command from the windows command shell:

mongodump --uri="mongodb://dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/?replicaSet=dd-domain-dev-shard-0" --db=TargetDatabase_dev --collection=abcCollection

When doing so, the following error occurs:

error parsing command line options: illegal argument combination: cannot specify --db and --uri

How can I create an acceptable command that includes both the database and collection? I must specify both because the replica set contains several databases, and I only want that single collection in the output file.

MongoDb version: 4.2.11 MongoDump version: r4.0.1

Upvotes: 1

Views: 2765

Answers (2)

user12582392
user12582392

Reputation:

The previous answer is correct but you will need to include most options in the --uri string.

If you want to keep those as dashed options (--option) you can do it by using --host instead.

mongodump --host="dd-domain-dev-shard-0/dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017" --db=TargetDatabase_dev 
--collection=abcCollection

You will probably need to authenticate though, like so

mongodump --host="dd-domain-dev-shard-0/dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/" --db=TargetDatabase_dev --collection=abcCollection --authenticationDatabase admin -u <username> -p <password> 

Here is the relevant MongoDB docs

Upvotes: 1

D. SM
D. SM

Reputation: 14520

Include the database name in the URI.

mongodump --uri="mongodb://dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/TargetDatabase_dev?replicaSet=dd-domain-dev-shard-0"  --collection=abcCollection

If that doesn't work:

mongodump --uri="mongodb://dd-domain-dev-shard-0-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-1-abcde.azure.mongodb.net:27017,dd-domain-dev-shard-2-abcde.azure.mongodb.net:27017/TargetDatabase_dev?replicaSet=dd-domain-dev-shard-0&authSource=admin"  --collection=abcCollection

Upvotes: 4

Related Questions