First, installation
Installation of MongoDB on window
1. Get System Version Information
Open cmd Run
WMIC OS Get Caption
WMIC OS get Osarchitecture
2. Download MongoDB
Currently the latest stable version 3.4.3
Directly double-click the installation and save the installation path in the system environment variable after installation is complete
3. Setting up the operating environment
Need a data directory to store all the data, the data directory format is \DATA\DB (will be useful later)
Then create a new \data\db folder directory in any location
and use the--dbpath [path] command to set this directory as the data store directory, and start Mongod.exe (run the program in CMD, so you can directly drag and drop the program into CMD.) You can also directly enter the path of the program as follows
".. \mongod.exe "--path" E:\mongoDBStore\data "[.. \ is the path where Mongod.exe]
Wait for output after executing command
Waiting for connections on port 27017
4. Start the Mongo.exe connection service
Mongo.exe
Output
Connecting to:mongodb://127.0.0.1:27017
MongoDB Server version:3.4.3
5. Open the browser input localhost:27017
It looks like you is trying to access MongoDB over HTTP on the native driver port.
Second, configure MongoDB as window service [after configuration, the service name is MongoDB]
# #1. Create a new Mongo.config file under Data directory (this file will be created automatically by the system later)
2. Create a new folder log on data directory and create a new file in it Mongodb.log
3. Edit the config file, assign values to both DBPath and LogPath, DBPath is the data folder that you created earlier, and LogPath is the path to the log file you created in the second step.
DBPath = E:\mongoDBStore\data
LogPath = E:\mongoDBStore\data\log\mongodb.log
4. Running in cmd
".. \mongod.exe "--config" E:\mongoDBStore\data\mongo.config "--install
5. Start the service cmd Run command
net start MongoDB
Command-line output: MongoDB service is starting ... Service has started to complete
6. Close the service
net stop MongoDB
7. Uninstall the service
Sc.exe Delete MongoDB
8. Start the Client Connection service
Direct double-click to run Mongo.exe
Or use MONGO mongodb://localhost:27017/[dbname in the cmd command line]
Three, the basic Operation 1. Basic Database Operations
View Database Show DBS
Select a database or create a new database use [database]
View all collections under the database Show collections
View current Database db
Delete current database Db.dropdatabase ()
Deletes a collection db in the database. [Collection].drop ()
2. Insert data to find data
Db. [Collection].insert ()
Db. [Collection].save ()
Db. [Collection].find ()
The above three methods represent the insertion and lookup of data in a database collection, respectively
The Find () method enables you to query a collection of data for a specified condition. You can then pass
. Count ()
. Skip ()
. Limit ()
. Sort ()
These four ways to filter the results of the query more closely
3. Updating data
The Save () method can either insert data or update data. Depending on ID field
Another way to update the data is update ()
To implement partial updates, you need to use $set syntax
Db.student.Insert({name:'Joyjoe', Age: -, Sex:'male'}) db.student.Insert({name:'Lucy', Age: -, Sex:'female'}) db.student.Insert({name:'Anna', Age: -, Sex:'female'})//Anna's age registration is wrong and needs to be modified db.student.Update({name:'Anna'}, {$Set: {Age: -} })//The registration information of Anna classmate was modified only to leave the age db.student.Update({name:'Anna'}, {age: -})
The third parameter of the update () method is used to indicate whether the new data will be inserted automatically when the data being found does not exist. Default false does not insert
The fourth parameter of the update () method is used to indicate whether to update all the data that is found. Default false to update only the first piece of data
4. Data deletion
Remove () Removes all data that meets the criteria by default
So the parameter is not passed in the system will be error
Set the second parameter justonly to true to delete only the first piece of data
Iv. Index
Queries all index DB in the collection. [Collection].getindexes ()
Add a new index to the collection (for an empty collection only)
Db. The [Collection].ensureindex () parameter is also a JSON object, the key indicates the index field, and the value indicates the sort direction.
Db. [Collection].createindex () create INDEX (recommended)
There are many indexes, namely: _ID index
1. _ID Index (collection default index)
2. Single key index (manual creation required) index data for the base data type
3. Multi-key index adds an index to each element in the array
4. Composite indexes create indexes for multiple field values that can support queries that match multiple keys
5. Expired indexes expire after a period of time, and the corresponding data is deleted. For data such as logging and user login information storage
Db.createindex ({}, {expireafterseconds:10}) (per second)
The value stored in the expired index must be the specified time type, and if an array is specified, the deletion is judged by the minimum value. Also cannot be a composite index, the deletion time is imprecise.
6. Full-Text Indexing
7. Location Index
Five, authority certification
Enable authentication in the Mongo.config file and configure it in a file
Auth = True
After restarting the service, you can find it in the log file.
Authorization: "Enabled"
No user has been created at this time and can still log on anonymously.
1. How to create a user
CreateUser () Incoming JSON object
{
User: "<name>",
PWD: "<cleartext password>",
CustomData: {<any information>},
Roles: [{role: ' <role> ', db: [' <database> ' ...]} ...]
}
Role:read, ReadWrite, DbAdmin, Dbowner, useradmin
[MongoDB] Getting Started notes