Download and installation of Mongodb

1, Download of MongoDB:

The official website of MongoDB is: 

MongoDB precompiled binary package download address:








You can download it on the official website, but I tried it. It's very troublesome. I found a simple way to download zip directly at this address,

The version above 4 has the problem of public key, so download the version below 4


2, MongoDB installation

Windows system   Current choice    win32/mongodb-win32-x86_64-2008plus-ssl-v3.6-latest-signed.msi 


Download the. msi file. After downloading, double-click the file and install it according to the operating instructions.




During installation, you can set your installation directory by clicking the "custom" button.






Next installation   "install mongoDB compass"   Uncheck (of course, you can also choose to install it, which may take longer to install). MongoDB Compass is a graphical interface management tool. We can download and install it on the official website later. The download address is:



Create data directory


MongoDB stores the data directory in the db directory. However, this data directory will not be created actively. We need to create it after installation.

You can use the command line to create the following

cd D:\Sql\MongoDB\Server\3.6
md "\data\db"

You can also create folders directly under windows Explorer

Under D:\Sql\MongoDB\Server.6, create two folders: dB and log. So far, there are the following folders under mongodb,



3, Run MongoDB server from the command line (startup mode 1)

In order to run the MongoDB server from the command prompt, you must execute from the bin directory of the MongoDB directory


Mongod: the 'mongod' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program.


[administrator mode] open CMD  

PS D:\Sql\MongoDB\Server\3.6\bin> mongod --dbpath D:\Sql\MongoDB\Server\3.6\data\db 





Indicates that the browser is opened after successful startup at port 27017   http://localhost:27017/   As shown below

be careful:

mongod --dbpath command is the storage location for creating database files. When starting mongodb service, you need to determine the storage location of database files first, otherwise the system will not create automatically and the startup will not succeed.

mongod  -- logpath indicates the path where the log file is stored     -- logappend   Indicates that the log file is written by appending


There will also be in the "bin/mongod.cfg" file   Configuration of dbPath and logPath

In addition:

Double click the mongod.exe file in the bin directory and it will flash out because the above steps are not executed   mongod --dbpath command

Double click the mongo.exe file in the bin directory to open the MongoDB client for database operation


4, The local client connects to the MongoDB server just built

The mongo command is to connect to the database service, that is, to the server, which can be accessed through the port (27017)

We can run mongo.exe in the command window to connect to MongoDB


Error: couldn't connect to server


Before connecting to MongoDB, you must run the command line in step 3 to run [MongoDB server starts first] before connecting to the local server





create profile

Create a profile. The file must set the systemLog.path parameter, including some additional configuration options.

For example, create a configuration file located in


Where systemLog.path and storage.dbPath are specified. The specific configuration contents are as follows:

    destination: file
    path: D:\Sql\MongoDB\Server\3.6\data\log\mongod.log
    dbPath: D:\Sql\MongoDB\Server\3.6\data\db


5, Install MongoDB service and start it (startup mode 2)

By executing mongod.exe, use the -- install option to install the service, and use the -- config option to specify the previously created configuration file.

D:\Sql\MongoDB\Server\3.6\bin\mongod.exe --config "D:\Sql\MongoDB\Server\3.6\mongod.cfg" --install

To use an alternate dbpath, you can specify it in the configuration file (for example: D:\Sql\MongoDB\Server.6\mongod.cfg) or on the command line through the -- dbpath option.

If necessary, you can install services for multiple instances of mongod.exe or mongos.exe. You only need to specify a different instance name by using -- serviceName and -- serviceDisplayName. Only when there are sufficient system resources and system design needs to do so.

mongod --dbpath=Installation path\data\db  --logpath=Installation path\data\log\mongodb.log --install --serviceName "MongoDB"



net start MongoDB
MongoDB service is starting
MongoDB service failed to start.

Service specific error occurred: 100




[reason 1] because the previous mongod has been started

[solution 1] turn off the mongod process


[reason 2] the mongod service is not shut down normally, resulting in the mongod being locked

[solution 2] enter the db folder, delete mongod.lock and storage.bson files, and then restart the service,






Start MongoDB service

net start MongoDB

Close MongoDB service

net stop MongoDB

Remove MongoDB service

D:\Sql\MongoDB\Server\3.6\bin\mongod.exe --remove


Run the MongoDB server from the command line    and    Configure MongoDB service   You can start it in any way.

Just choose one operation


6, MongoDB background management Shell

If you need to enter the mongodb background management, you need to first open the bin directory under the mongodb installation directory, and then execute the mongo.exe file. The MongoDB Shell is an interactive Javascript shell provided by mongodb, which is an interactive environment for operation and management of mongodb.

When you enter the mongoDB background, it will link to the test document (database) by default:

MongoDB shell version v3.6.23-8-gc2609ed3ed
connecting to: mongodb://
Implicit session: session { "id" : UUID("7c838f33-6a05-4894-827d-76713c404e4e") }
MongoDB server version: 3.6.23-8-gc2609ed3ed

Because it is a JavaScript shell, you can run some simple arithmetic operations:

> 2 + 2

db   Command to view the document (database) of the current operation:

> db

Insert some simple records and find it:

> db.runoob.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.runoob.find()
{ "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 }

The first command inserts the number 10 into the x field of the runoob set.





7, Operation database

At this stage, the database has successfully run. The next step is to operate some commands to insert data into the database, and you can see the results of a series of operations on the database

db.stats()   The command displays the database name, the number of collections and documents in the database

> db.stats()
        "db" : "test",
        "collections" : 1,
        "views" : 0,
        "objects" : 1,
        "avgObjSize" : 33,
        "dataSize" : 33,
        "storageSize" : 16384,
        "numExtents" : 0,
        "indexes" : 1,
        "indexSize" : 16384,
        "fsUsedSize" : 15998328832,
        "fsTotalSize" : 43689963520,
        "ok" : 1
} will list a list of commands

DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
        db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
        db.auth(username, password)
        db.cloneDatabase(fromhost) - deprecated
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost) - deprecated
        db.createCollection(name, {size: ..., capped: ..., max: ...})
        db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
        db.currentOp() displays currently executing operations in the db
        db.eval() - deprecated
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getSiblingDB(name) get the db at the same server as this one
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into {cmdObj: 1}
        db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
        db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
        db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.version() current version of the server


8, The difference between mongod and mogo

        1. Pay attention to the difference between mongod and mongo

The former is to enable MongoDB process (server), and the latter is to connect MongoDB (client)

2. To execute mongod, you need to configure the path to enable it. Simply double-click the mongod.exe file to flash. The correct thing is to execute the mongod --dbpath command

3. Execute mongod command to enable MongoDB process (server), and then use mongo to connect it,


9, Several concepts

source   MongoDB interview questions and reference answers

8. What is "mongod"“
Mongod is the main process of handling MongoDB system. It processes data requests, manages data storage, and performs background management operations. When we run mongod command, it means that MongoDB process is starting and running in the background.

9. What are the "mongod" parameters
Pass the database storage path. The default is "/ data/db"
The default port number is "27017"
10. What is "mongo"
It is a command-line tool used to connect a specific mongod instance. When we run the mongo command without parameters, it will use the default port number and localhost to connect


Tags: Database

Posted on Tue, 30 Nov 2021 15:32:28 -0500 by rline101