Data is every front-end people must contact the same, all the data are back-end to write, if they want to practice the project, but no data, but write some false data, to write, or build a data through json-server, today we will build a real database through MongoDB, From the installation to the configuration to the writing, one step, is also the small series installation configuration process, the problems encountered, install the following steps, step by step, I believe you will succeed
What is MongoDB?
MongoDB is written by the C + + language and is an open source database system based on distributed file storage.
In the case of high load, adding more nodes can guarantee the performance of the server.
MongoDB is designed to provide scalable, high-performance data storage solutions for Web applications.
MongoDB stores data as a document and data structures consist of key-value (key=>value) pairs. A MongoDB document is similar to a JSON object. Field values can contain other documents,
The next small series with you to complete the installation and configuration step-by-step
1. Installing MongoDB
After downloading the installation package, the installation package is placed in the location where you want to put it, and the installation package is placed on the G-Drive.
Always perform next next, when entering this interface, the following √ must be removed, or it will be installed particularly slow, if not removed, may have to wait a few hours above, small series has been tested
When all the step values are completed, find your installed directory, there will be the following results, (small series of mosaic of that, no tube, will not be affected, just small part of the installation package is also placed here)
2. Environment variable Configuration
When MongoDB is installed, you need to configure the environment variables,
right mouse button Select "Computer" "Properties", will pop up a box, click "Advanced System Settings", will pop up a system properties, find environment variables
Locate the path in the system variable, click Edit
Find your own installed directory, be sure to enter the bin directory, copy this directory into the variable value, followed by a ";", and then click OK, so that the environment variables are configured
3. Where to create the database file
In your data directory, create a DB file G:\mongodb\data\db
Because you need to create a storage folder for the database files before you start the MongoDB service, the command is not created automatically and cannot be started successfully
4. Start MongoDB Service
1. Open cmd command line guard with Windows+r key to open, enter cmd
2. Enter the G-disk, which is the directory you installed, G:\mongodb\bin; (Be sure to enter the G-disk first, let the CD to your directory CD MongoDB, and then in the CD bin this directory)
3. Enter the following command to start Mongodb,monogd--dbpath g:\mongodb\data\db, this is the directory you installed, and then enter
That is, the database is created in the third step under the file path to start. When you click Enter, the interface appears, indicating that it has been successful.
4. In the browser input http://localhost:27017, if in the browser appears the following paragraph English explanation succeeds
V. Configuring the local Windows MongoDB service
This can be set to power-on self-boot, can be directly started off manually, can be started by command line net start MongoDB. This configuration is much more convenient. And do not start in the directory that enters the bin.
1. First create a new folder under the data file log (to hold the log file)
2. In MongoDB new configuration file Mongo.config, this is the same as the bin directory sibling
3. Open Mongo.config with Notepad and enter the following two commands, then save
Dbpath=g:\mongodb\data\db
Logpath=g:\mongodb\data\log\mongo.log Mongo.log This will be generated automatically
4. Open cmd as an administrator, the top left corner will appear the administrator three words, and then once into your bin directory G:\mongodb\bin, this must have an administrator's identity to open, or execute the following command will always error
Input: Mongod-dbpath "g:\mongodb\data\db"-logpath "G:\mongodb\data\log\mongo.log"-install-servicename "MongoDB", MONGO DB is the name of the startup.
If there is an error in the input command, first delete the service SC delete MongoDB and enter the last command again.
In this case, the MongoDB service Windows has been configured so that we can start mongodb without going into the bin directory,
When we started by entering net start MongoDB, I reported this error,
Workaround
Find this directory in mongodn>data>db, delete mongodb.lock this file,
After deleting this file, run the net start MongoDB command again, the service is ready to start, the MongoDB is already configured
We can open a service window by entering services.msc in the command window to see the MongoDB service start-up
Six common commands
net start MongoDB Open service
NET start MongoDB shutdown service
Seven Create a database table
Complete the above configuration we can build a database, as long as the above step by step, will be successful, small series is also stepping on a lot of pits, summed up,
A common command for establishing a database, which must be remembered
In the Bin directory, enter MONGO start, start writing data content
Show DBS shows all current data, you can see that there is currently no data content
Use students create database name, students is database name
DB Displays the current database
Show collections displays all the collections in the database
Db.<collection>.insert (DOC) inserting documents into the database
Db.students.insert ({ID: "001", Name: "JIM", Age: "18"}); This is the data we inserted.
Db.students.find () Through the data This command, to students this database query the current collection of all the documents, can also be conditional query
All of the above are all today's content, from this can also build data, to do some operations, if you like, you can pay attention to, in the installation process such as encountered problems, you can leave a message, convenient to answer with you
The most comprehensive MongoDB installation and configuration