Raymond Raymond / Tools

Install MongoDB on WSL

event 2021-02-01 visibility 3,141 comment 0 insights toc
insights Stats

MongoDB  is one of the most famous NoSQL databases that uses JSON-like documents with optional schemas. 


Before installing MongoDB on WSL, please ensure you have WSL enabled on your Windows 10 system. 

Follow Install Windows Subsystem for Linux on a Non-System Drive to install WSL on a non-C drive. 

This tutorial provides steps to install MongoDB on WSL Debian distro. You can follow similar steps to install it in other Linux distros. 

Step by step guide

  1. Open Debian distro through WSL command:
    wsl -d Debian
  2. Update Debian packages:
    sudo apt update

    You need to type user password.

  3. Install Redis using the following command:
    sudo apt install mongodb

    Type Y to continue when asked.

  4. Wait until the installation is completed. 
  5. Verify MongoDB version:
    $ mongod --version
    db version v3.2.11
    git version: 009580ad490190ba33d1c6253ebd8d91808923e4
    OpenSSL version: OpenSSL 1.0.2l  25 May 2017
    allocator: tcmalloc
    modules: none
    build environment:
        distarch: x86_64
        target_arch: x86_64

By default, MongoDB stores data in /data/db and host the daemon service on port 27017. And mongod is the daemon (host process for the database) and mongo is the command-line shell that connects to a specific instance of mongod.

Test MongoDB server

  1. Start MongoDB using the following command:
    sudo service mongodb start
    # or
    sudo /etc/init.d/mongodb start
  2. Check the status of the server
    sudo service mongodb status
    # or
    sudo /etc/init.d/mongodb status
    The command prints out the following text:
    [ ok ] Checking status of database: mongodb running.
  3. Use mongo CLI:
    $ mongo
    MongoDB shell version: 3.2.11
    connecting to: test
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, see
    Questions? Try the support group
    > help
            db.help()                    help on db methods
            db.mycoll.help()             help on collection methods
            sh.help()                    sharding helpers
            rs.help()                    replica set helpers
            help admin                   administrative help
            help connect                 connecting to a db help
            help keys                    key shortcuts
            help misc                    misc things to know
            help mr                      mapreduce
            show dbs                     show database names
            show collections             show collections in current database
            show users                   show users in current database
            show profile                 show most recent system.profile entries with time >= 1ms
            show logs                    show the accessible logger names
            show log [name]              prints out the last segment of log in memory, 'global' is default
            use <db_name>                set current database
            db.foo.find()                list objects in collection foo
            db.foo.find( { a : 1 } )     list objects in foo where a == 1
            it                           result of the last line evaluated; use to further iterate
            DBQuery.shellBatchSize = x   set default number of items to display on shell
            exit                         quit the mongo shell
    > show dbs
    local  0.000GB
  4. Type exit to quite mongo CLI.
  5. Run the following command to stop Mongo daemon service:
    sudo /etc/init.d/mongodb stop
    # or
    sudo service mongodb stop

Create objects

Using mongo CLI to insert some documents:

use app
   { "_id" : "1",
     "firstName" : "Raymond",
     "lastName" : "Tang",
     "roles" : ["user","admin"]
   { "_id" : "2",
     "firstName" : "Kontext",
     "lastName" : "",
     "roles" : ["admin"]

The above code snippets creates a collection named users in database app. Two user records are inserted into the collection.

Print out the users

> db.users.find()
{ "_id" : "1", "firstName" : "Raymond", "lastName" : "Tang", "roles" : [ "user", "admin" ] }
{ "_id" : "2", "firstName" : "Kontext", "lastName" : "", "roles" : [ "admin" ] }

The above command prints out the documents in the collection.

Fix issue

If mongod cannot be started successfully, try run the following command to repair and then restart the service:

sudo mongod --repair
sudo service mongodb start

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts