Mongodb on ubuntu 20.04




  • Install
$ wget -qO - | sudo apt-key add -
$ echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

$ sudo apt-get update
$ sudo apt-get install -y mongodb-org
  • Config data directory
$ sudo vim /etc/mongod.conf

# Where and how to store data.
#  dbPath: /var/lib/mongodb
  dbPath: <YOUR_PATH>
    enabled: true


$ sudo systemctl daemon-reload
$ sudo systemctl start mongod
$ sudo systemctl status mongod


  • mongodump
$ mongodump --help
  mongodump <options> <connection-string>

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 collection.

Connection strings must begin with mongodb:// or mongodb+srv://.

See for more information.
namespace options:
  -d, --db=<database-name>                                  database to use
  -c, --collection=<collection-name>                        collection to use

$ mongodump -d <database-name>	# output in dump folder
  • mongorestore
$ mongorestore --help
  mongorestore <options> <connection-string> <directory or file to restore>

Restore backups generated with mongodump to a running server.

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

$ mongorestore -d <database-name> dump/<database-name>
  • mongosh (the “mongo” shell has been superseded by “mongosh”)

$ mongosh --help

  $ mongosh [options] [db address] [file names (ending in .js or .mongodb)]

        --shell                                Run the shell after executing files
        --nodb                                 Don't connect to mongod on startup - no 'db address' [arg] expected
        --norc                                 Will not run the '.mongoshrc.js' file on start up
        --eval [arg]                           Evaluate javascript
        --retryWrites                          Automatically retry write operations upon transient network errors

  DB Address Examples:

        foo                                    Foo database on local machine                        Foo database on machine                   Foo database on machine on port 9999
        mongodb://         Connection string URI can also be used

  File Names:

        A list of files to run. Files must end in .js and will exit after unless --shell is specified.


        Start mongosh using 'ships' database on specified connection string:
        $ mongosh mongodb://

  For more information on usage:

### Some usage
$ mongosh
  Connecting to: mongodb://

test> show dbs
  admin           41 kB
  config        73.7 kB
  fdl_database  9.39 MB
  local         73.7 kB
  test           270 kB

test> use fdl_database
  switched to db fdl_database
fdl_database> show collections