1. Installing MongoDB server
Here you look directly at my previous blog post "How to properly install MongoDB on Windows";
In fact, it only takes four steps to install a MONGODB server:
1, to the official website to download MongoDB, and decompression;
2, add the/bin directory to the system path;
3, create the data file path, that is<mongo_data_location>/data/db ;
2. Start MongoDB in the console:
mongod -dbpath <mongo_data_location>/data/db
Mongod command-line arguments:
Parameters |
Description |
Help,-H |
Return basic Help and usage text |
–version |
Returns the MongoDB version number |
–config < filename >,-f < file name > |
Specify the configuration file that contains the run-time configuration |
–verbose,-V |
Increase the number of internal reports sent to the console and written to the –logpath specified log file |
–quiet |
Reduce the number of reports sent to the console and log files |
–port < ports > |
Specifies MongoDB to listen for the IP address of the client's link, the default value is 27017 |
–maxconns < numbering > |
Specifies the number of concurrent connections that the Mongod accepts, with a maximum value of 20000. |
–logpath < paths > |
Specifies the log file path. The log file will be overwritten when you restart, unless you also specify –logappend |
–auth |
Use database authentication for users who connect to the database from a remote host. |
–dbpath < paths > |
Specifies the directory that the Mongd instance uses to store its data |
–nohttppinterface |
Disabling the HTTP interface |
–nojournal |
Disable logging |
–noprealloc |
Prevents pre-allocating data files, which reduces startup time. However, there may be significant performance losses during normal operation. |
–repair |
Run the hotfix on all databases |
–bind_ip |
Specifies the IP address of the Mongod binding and listening connection, the default is all Interface (all interfaces) |
3. Start Mongod
-dbpath <mongo_data_location>/data/bin 或者mongod -port 28008 -dbpath<mongo_data_location>/data/bin*
4. Stop MongoDB
Open the MongoDB Shell client (execute the following command in the console window to open the shell client):
$ mongo
When the shell is opened, the specified database is closed in the following form:
use admin // 切换到指定数据库,这里是admin数据库 db.shutdownServer() // 立即关闭admin数据库
5. How to access MongoDB from the shell client.
Once you enter the MongoDB shell, you can manage all aspects of MongoDB. Note that the MongoDB shell is JavaScript-based, so it can also support most JavaScript syntax. In addition, the shell provides direct access to databases and collections on the server. Therefore, the changes in the shell and the tasks that are performed will directly affect the data on the server and its performance.
MongoDB's shell command:
1. <option>
Help
Display MongoDB syntax help;
2. <database>
use
Change the MongoDB handle;
3. Show<option>
3-1、 dbs 3-2、collections 3-3、profile
Displays the list according to the option parameter;
- Display database inventory;
- Displays the list of collections for the current database;
- Displays the latest System.profile entry with a time exceeding 1 milliseconds;
- Displays the last paragraph of the login memory. If name is not specified, global is used by default.
4. Exit
Exit the database;
MongoDB Shell method:
1>, load (script)
Load and run JavaScript inside the shell. Using this method is most appropriate when scripting a database.
2>, UUID (String)
Converts a 32-byte hexadecimal string into a bson uuid
3>, Db.auth (username, password)
In the current database, you are authenticated
Complete list: https://docs.mongodb.org/manual/reference/method/#native
to understand the parameters and results of a command:
The MongoDB Shell is an interactive JavaScript shell that closely integrates with MongoDB's data structure.
This means that much of the data interaction, from the parameters passed to the method to the data returned from the method, is a standard MongoDB document, which in most cases is just a JS object.
Create the user with the following code, and define the user by passing in these parameters:
db.createUser({ "G-Dragon", "test", "read" ], "readWrite" ] }})>db.system.users.find()
using the shell to write MongoDB scripts
There are two ways to run the MongoDB shell script:
1>, in the command line, use –eval.
mongo test --eval "printjson( db.getCollectionName() )"
2>, run the MONGOBD shell script using the load (Script_path) method.
This method loads a JS file and is executed immediately.
load("/tmp/db_update.js" // shell命令,加载并执行db_update.js脚本文件
[MongoDB Learning Note-01] Getting Started