Edited 3 weeks ago by ExtremeHow Editorial Team
InstallationSetupWindowsDatabaseMongoDBSoftwareDeploymentDevelopmentConfigurationOperating System
This content is available in 7 different language
MongoDB is a popular NoSQL database that helps developers store large amounts of data efficiently. This guide will walk you through the step-by-step process of installing MongoDB on a Windows operating system. By the end of this guide, you will have a working MongoDB setup and understand the basics of running MongoDB on your Windows system.
The first step to install MongoDB is to download the MongoDB installer for Windows. Follow these steps:
Once the MongoDB installer file is downloaded, you need to install it on your Windows system. Follow these steps:
.msi
file to launch the MongoDB installer.To use MongoDB's command-line tools in a convenient way, you can add the MongoDB bin directory to your system's PATH environment variable. This step allows you to run MongoDB commands from any command prompt without specifying the full path. You can do it as follows:
environment variables
, then click "Edit system environment variables".bin
directory. By default, this is C:\Program Files\MongoDB\Server\version_number\bin
.MongoDB needs directories to store data and log files. By default, these directories are not created during installation, so you must create them manually. Here's how:
data
by right-clicking, choosing "New" and then "Folder."data
folder, create another folder named db
. This is where MongoDB will store its database files.log
folder within data
directory for MongoDB's log files, although this is not mandatory as logs can be directed to specific files.Now that MongoDB is installed and your directories are set up, it's time to run MongoDB. You can start the MongoDB server either as a Windows service or manually. This guide explains both methods:
Since MongoDB was installed as a service, it starts automatically with Windows. However, you can also manage it manually:
cmd
, right-clicking the "Command Prompt" app, and selecting "Run as administrator."net start MongoDB
and press Enter. This starts the MongoDB service.net stop MongoDB
command when required.If you prefer to run MongoDB manually from the command line, follow these steps:
bin
directory by typing the following command and pressing Enter: cd C:\Program Files\MongoDB\Server\version_number\bin
. Replace version_number with your installed MongoDB version.mongod
in the command prompt. If your data directory is not the default C:\data\db
, specify the path by adding --dbpath
option e.g.: mongod --dbpath=path_to_your_db_directory
.With MongoDB running, you can now connect to the database using the MongoDB shell, which is a command-line tool for interacting with the MongoDB database. Here's how to use it:
bin
directory is in your PATH, just type mongo
and press Enter. If not, navigate to bin
directory and run mongo
from there.use myDatabase
and pressing Enter. This creates (or switches to) a database named "myDatabase".Now that MongoDB is up and running, it's useful to know some basic commands to manage and work with your data. Here are some basic operations in MongoDB:
Create a new database by typing the use myDatabase
command. If the database doesn't exist, MongoDB will create it the first time you store data into it.
To add data to your database, insert documents into a collection. A collection is similar to a table in a relational database. Use the following command in the MongoDB shell:
db.myCollection.insertOne({ "name": "John Doe", "Age": 29, "city": "New York" })
This command adds a new document to the collection "myCollection". If the collection does not exist, MongoDB creates it.
Retrieve data using the find
command:
db.myCollection.find({ "name": "John Doe" })
This command searches for documents where the field "name" is equal to "John Doe".
Update a document with updateOne
or updateMany
command:
db.myCollection.updateOne( { "name": "John Doe" }, { $set: { "Age": 30 } } )
This updates the "Age" field to 30 for documents where the "Name" is "John Doe".
Delete a document using deleteOne
or deleteMany
:
db.myCollection.deleteOne({ "name": "John Doe" })
This command deletes the document where the "name" is "John Doe".
You have learned how to install MongoDB on your Windows computer, and now you have a basic understanding of how to start MongoDB, connect using the MongoDB shell, and perform basic operations. MongoDB is a powerful NoSQL database with a flexible and scalable architecture. As you dive deeper into MongoDB, explore its advanced features such as indexes, aggregation, and replication to build robust and high-performance applications.
If you find anything wrong with the article content, you can