Using mongodump with a standard Mongo URI

Viewed 22410

The mongo client can connect with a standard URI:

mongo mongodb://<dbuser>:<dbpassword>@<server>:<port>/<db>

However, mongodump seems to require an awkward syntax breaking this up into different arguments:

mongodump -u dbuser -p dbpassword -h server -p port -d db ...

Is there a quick and easy way to pass a URI to mongodump as well?

5 Answers

The --uri option was added within a minor release of MongoDB 3.4.6. This was referenced in the JIRA issue TOOLS-1587.

It actually did not get official documentation until the MongoDB 3.6 release, but it's now in the manual page

--uri
New in version 3.4.6.

Specify a resolvable URI connection string for the mongod to which to connect.

The following is the standard URI connection scheme:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

For detailed explanations of the components of this string, refer to the Connection String URI Format documentation.

The same --uri option is added to other tools such as mongoexport, mongoimport and mongorestore.

Answer: 05-15-2020

I know this is a late answer, but it solved my problem.
To dump using an URI, do:

mongodump --uri=[uri]/[db]

where:

  • uri is your URI (e.g. mongodb+srv://john:xxxxxxxxxxxxxxx@cluster0-jdtjt.mongodb.net)
  • db is the database that you want to dump (e.g. sales)

So in this artificial example, it would be like this:

mongodump --uri=mongodb+srv://john:xxxxxxxxxxxxxxx@cluster0-jdtjt.mongodb.net/sales

So if this really did not work, edit the file /etc/resolv.conf and change nameserver value to e.g. 8.8.8.8, like this:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

# nameserver 127.0.0.53
nameserver 8.8.8.8
options edns0

Now it should works.

Easy steps to getting dump and restore whole database at once with any data loses

take dump in the local machine

mongodump --uri "mongodb+srv://username:password@dbexmplae-fsddf.igt.mongodb.net/dbname" --out "C:\databaseDump"

**locate to the dumped folder and restore to local database **

mongorestore --db dbname C:\databaseDump\dbname

restore to remote database

mongorestore --uri "mongodb+srv://username:password@dbexmplae-fsddf.igt.mongodb.net/dbname" --db dbname C:\databaseDump\dbname

A simple and easy string to understand

mongodump --uri='mongodb://...url/...db-name' --out $(pwd)

or

mongodump --uri='mongodb+srv://...url/...db-name' --out $(pwd)

Depending on your implementation, will dump the whole database of $(db-name) located at $(url) and place the contents into your current working directory $(pwd)

Even if you supply the uri option, you still need to supply the db option. The documentation at https://docs.mongodb.com/manual/reference/program/mongorestore/ is incorrect when it says that the db option is "incompatible" with the uri option. If you don't supply the db option together with the uri option you get a don't know what to do with file error for each .bson file that you're trying to restore. I tested using mongorestore version 3.6.9 on ubuntu and from mac osx.

Related