I am trying to export data from local database using:

mongodump --db ride4you --collection rides

But I receive the next error:

Failed: error counting ride4you.rides: not authorized on ride4you to execute command { count: "rides", query: {} }

What is this and what to do? Plus, is there a way to export the whole database at once, instead by each table?

  • @Raz, Could you elaborate more , how you are doing mongodump? – Md Haidar Ali Khan Nov 14 '17 at 12:39
  • Yes, I have ran mongod --auth and earlier I created a user with a password. Now when I am trying to do any mongo comment, beside of use, like db.getUsers() or db.dropUser() I have receive an error due to not authorized. Well, how do I authorize then? – Raz Nov 14 '17 at 12:48
  • @Raz, You can change authorization through db.changeUserPassword(username, password). You must ensure that you are using your correct database. I mean (use ride4you) through mongo command. And ride4you should be current database.docs.mongodb.com/manual/reference/method/db.changeUserPassword – Md Haidar Ali Khan Nov 14 '17 at 13:31
  • @Raz, if you Allows a user to authenticate to the database from within the shell through db.auth(). If you are getting authentication error through mongo shell then you can change the user password through db.changeUserPassword(). – Md Haidar Ali Khan Nov 14 '17 at 13:42

@Raz, I have gone through your mongodump statement. You are doing mistake.

mongodump --db ride4you --collection rides

As per your mongodump code , As i am able to understand in your mongodump code, there is database name (ride4you) and collection name (rides) without the path of dump (.JSON) file.

Let's start from beginning. As per MongoDB BOL , if you are in BIN directory . Just type along with mongodump --help. You shall get the mongodump command all details. Which i am copy and paste from my environment.

C:\Program Files\MongoDB\Server\3.2\bin>mongodump --help
  mongodump <options>

Export the content of a running server into .bson files.

Specify a database with -d and a collection with -c to only dump that database or

See http://docs.mongodb.org/manual/reference/program/mongodump/ for more informat

general options:
      /help                                                 print usage
      /version                                              print the tool
                                                            version and exit

verbosity options:
  /v, /verbose:<level>                                      more detailed log
                                                            output (include
                                                            multiple times for
                                                            more verbosity,
                                                            e.g. -vvvvv, or
                                                            specify a numeric
                                                            value, e.g.
      /quiet                                                hide all log output

connection options:
  /h, /host:<hostname>                                      mongodb host to
                                                            connect to

                                                            2 for replica sets)
      /port:<port>                                          server port (can
                                                            also use --host

authentication options:
  /u, /username:<username>                                  username for
  /p, /password:<password>                                  password for
      /authenticationDatabase:<database-name>               database that holds
                                                            the user's
      /authenticationMechanism:<mechanism>                  authentication
                                                            mechanism to use

namespace options:
  /d, /db:<database-name>                                   database to use
  /c, /collection:<collection-name>                         collection to use

query options:
  /q, /query:                                               query filter, as a
                                                            JSON string, e.g.,
      /queryFile:                                           path to a file
                                                            containing a query
                                                            filter (JSON)
      /readPreference:<string>|<json>                       specify either a
                                                            preference name or
                                                            a preference json
      /forceTableScan                                       force a table scan

output options:
  /o, /out:<directory-path>                                 output directory,
                                                            or '-' for stdout
                                                            (defaults to 'dump')
      /gzip                                                 compress archive
                                                            our collection
                                                            output with Gzip
      /repair                                               try to recover
                                                            documents from
                                                            damaged data files
                                                            (not supported by
                                                            all storage engines)
      /oplog                                                use oplog for
                                                            taking a
      /archive:<file-path>                                  dump as an archive
                                                            to the specified
                                                            path. If flag is
                                                            specified without a
                                                            value, archive is
                                                            written to stdout
      /dumpDbUsersAndRoles                                  dump user and role
                                                            definitions for the
                                                            specified database
      /excludeCollection:<collection-name>                  collection to
                                                            exclude from the
                                                            dump (may be
                                                            specified multiple
                                                            times to exclude
      /excludeCollectionsWithPrefix:<collection-prefix>     exclude all
                                                            collections from
                                                            the dump that have
                                                            the given prefix
                                                            (may be specified
                                                            multiple times to
                                                            exclude additional
  /j, /numParallelCollections:                              number of
                                                            collections to dump
                                                            in parallel (4 by

Mostly i am doing mongodump in my environment like

For a single database dump

mongodump -d myfirstdb -o C:\Test\TestDB.json

For all database dump at a time

mongodump --verbose C:\Test\

Here In first statement myfirstdb is database in my MongoDB Server. -o is for output directory options and C:\Test is a folder where TestDB.json my dump (.JSON) File reside.

In second statement i have used --verbose . for more details output

Note: mongodump import all dump (.JSON) files in MongoDB Server at a time. But when doing mongoimport then it import one dump (.JSON) file at a time.

For Further your ref mongodump

  • not a full answer: he is attempting to dump a single collection not the entire DB as you do. Also to pass a name terminating with .json as you do in the "-o C:\Test\TestDB.json" part makes little sense since the name you provide will be used to create a folder under which mongodump will put the collections in separate bson/json files – user1708042 Jun 25 '19 at 14:11

There are probably two errors here.

When you created user and enabled authentication mongod --auth, your first created user was NOT root/administrator (i.e. super user) who could do anything (like create new user with proper rights). Now you are stuck with user who don't have all rights.

Second, if your mongod is running at authentication mode, you need always authenticate when you use mongodump. So you need to give parameters -u (user name) and -p (password) so that database knows who is connecting and do that user have authorization to dump that database.

  • Probably first situation, how can be solved? – Raz Nov 15 '17 at 11:28
  • Start your mongod to maintenace mode, i.e. without --auth. Then start mongo and give commands use admin db.createUser( { user: "superuser", pwd: "12345678", roles: [ "root" ] } ) what create superuser. Restart your mongod again with --auth and use that user to dump mongodump -u superuser -p 12345678 --db ride4you --collection rides --out dumpfile.dump OR use that superuser to create dump user with backup role. (of course, when you are creating that superuser, you can do that backup user same time. – JJussi Nov 15 '17 at 15:59

I figured that the collection was not existing in the database, once I created the collection the command went through fine.

So, basically the error "invalid namespace specified" means that the collection to export ".", in which "your_collection" comes as blank.

  • 1
    In my case the collection existed and I simply misspelled its name. So when "invalid namespace specified" is returned as an error one should first check both the "database name" and the "collection name". The namepace is indeed "<db_name>.<collection_name>" – user1708042 Jun 25 '19 at 14:50

I met the same error output when trying to do mongo dump for a collection not existing in that database. Not sure why it is stated as "invalid namespace" instead of collections.

Also, I think the parameter should be '-d' instead of 'db'.

  • ,I think the parameter should be '-d' instead of db. YES. As already I have been mentioned in my answer. – Md Haidar Ali Khan Apr 23 '19 at 4:14
  • the "usage" is not clear on this: on my installation (version 4.0.1 in windows): -d <dbname> --db <dbname> and /db:<dbname> are all accepted – user1708042 Jun 25 '19 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.