w3resource

Install MongoDB on Windows

Install MongoDB Community Edition and Compass on Windows

MongoDB is a leading NoSQL database known for its flexibility and scalability. Installing the MongoDB Community Edition and its GUI tool, MongoDB Compass, on Windows is a straightforward process. This guide walks you through each step, from downloading to verifying your setup, while also addressing common troubleshooting tips and additional insights.

Step-by-Step Installation Guide

1. Pre-Installation Checklist

Before installing MongoDB, ensure your system meets the following requirements:

  • Supported Windows Versions: Windows 10 (64-bit), Windows 11, Windows Server 2016/2019/2022.
  • Hardware: Minimum 2GB RAM (4GB recommended); sufficient disk space for data storage.

2. Download and Install MongoDB Community Edition

  • Visit MongoDB's Website
  • Download the Installer
    • Select "Windows" as your OS and download the MSI package.
  • Run the Installer
    • Locate the downloaded MSI file and double-click it to launch the setup wizard.
    • Choose the "Complete" setup option.
  • Install as a Service (Optional)
    • Opt to run MongoDB as a Windows service for automatic startup.
  • Finalize Installation
    • Complete the wizard and close the installer.

3. Configure MongoDB Environment

  • Set Up the Data Directory
    • MongoDB uses C:\data\db by default to store databases.
    • Create the directory manually if it doesn’t exist.
  • Update PATH Environment Variable
    • Add MongoDB’s bin directory to the system PATH for easy access to MongoDB tools.
  • Verify Installation
    • Open Command Prompt and run:
    •  mongo --version

      If the version number appears, the installation was successful.

4. Install and Use MongoDB Compass

  • Download Compass
    • Visit the MongoDB Compass page.
    • Download the installer for Windows.
  • Install Compass
    • Run the installer and follow the on-screen instructions.
  • Connect Compass to MongoDB
    • Launch Compass and enter localhost:27017 in the connection string field.
    • Click "Connect" to access your databases.

5. Starting MongoDB

  • Run the MongoDB Server
    • Open Command Prompt, navigate to the bin directory, and run:
    mongod
    
  • Access MongoDB Shell
    • Open a new terminal and type:
    • mongo
      
  • Verify Functionality
    • Insert a test document into a collection and retrieve it:
    • db.test.insertOne({ name: "Test", status: "Success" })
      db.test.find()
      

    Troubleshooting Installation Issues

    • Error: mongo is not recognized
      • Ensure MongoDB’s bin directory is in the PATH environment variable. Restart Command Prompt after updating.
    • "Data directory not found" error
      • Verify the existence of C:\data\db. Create it if missing.
    • Compass fails to connect
      • Check if the MongoDB server (mongod) is running.

    Uninstalling MongoDB

    • Stop the MongoDB Service
      • Open services.msc, locate MongoDB, and stop the service.
    • Uninstall MongoDB
      • Go to Control Panel > Programs > Uninstall a Program.
    • Clean Remaining Files
      • Delete the installation folder and the data directory (C:\data\db).
    • Remove PATH Entries
      • Remove MongoDB references from the PATH variable.

    Additional Commands for Beginners

    Code:

    
    # Insert multiple documents
    db.users.insertMany([
      { name: "Blythe", age: 28 },
      { name: "Bert", age: 35 }
    ])
    # Find all documents
    db.users.find()
    # Update a document
    db.users.updateOne({ name: "Blythe" }, { $set: { age: 29 } })
    # Delete a document
    db.users.deleteOne({ name: "Bert" })
    
    

    Become a Patron!

    Follow us on Facebook and Twitter for latest update.

    It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

    https://www.w3resource.com/mongodb/installation-windows.php