first, to install MongoDB
1.:http://www.mongodb.org/downloads
2. Unzip to the directory you want to install, such as D:\mongodb
3. Create folders d:\mongodb\data\db, D:\mongodb\data\log, respectively, to install the DB and log files, create a log file under the log folder MongoDB.log, that is, d:\mongodb\data\ Log\mongodb.log
4. Run cmd.exe into the DOS command interface and execute the following command
> CD D:\mongodb\bin
> D:\mongodb\bin>mongod-dbpath "D:\mongodb\data\db"
If you see a similar message, the boot succeeds, and the default MongoDB listener port is 27017,mysql 3306
5. Test the connection
Open a new CMD window, enter the MongoDB bin directory, enter MONGO or Mongo.exe, the following message shows that the test passed, at this time we have entered the test database, how to enter the other database below will say.
Enter exit or CTRL + C to exit.
6. When the Mongod.exe is turned off, Mongo.exe will not be able to connect to the database, so each time you want to use the MongoDB database to open the Mongod.exe program, it is more troublesome, at this time we can install MongoDB as a Windows service
Or run cmd, go to the Bin folder and execute the following command
> D:\mongodb\bin>mongod--dbpath "d:\mongodb\data\db"--logpath "D:\mongodb\data\log\MongoDB.log"--install-- ServiceName "MongoDB"
Here MongoDB.log is the log file that was started,--servicename the "MongoDB" service named MongoDB
Then start the MongoDB service
> D:\mongodb\bin>net START MongoDB
Open Task Manager and you can see that the process has started
7. Shutting down services and deleting processes
> D:\mongodb\bin>net Stop MongoDB (Close service)
> D:\mongodb\bin>mongod--dbpath "d:\mongodb\data\db"--logpath "D:\mongodb\data\log\MongoDB.log"--remove-- ServiceName "MongoDB" (delete, note not--install)
Ii. Use of MongoDB
1. Common commands
- Show DBS display database list
- Use dbname into the dbname database, case sensitive, no this database does not matter
- Show Collections displays a collection in the database, equivalent to a table
2. Create & Add
- Db.users.save ({"Name": "LECAF"}) created a collection named users and added a new {"Name": "LECAF"} data
- Db.users.insert ({"Name": "Ghost", "Age": ten}) inserts a new data into the Users collection, and if there is no users this collection, MongoDB automatically creates
- There is a slight difference between save () and insert (): If the new data primary key already exists, insert () will not do the operation and prompt the error, while save () changes the original content to new content.
- Existing data: {_id:1, ' name ': ' N1 '}, _id is the primary key
- Insert ({_id:1, "name": "N2"}) will prompt for an error
- Save ({_id:1, "name": "N2"}) will change the N1 to N2, with the function of update.
3. Delete
- Db.users.remove () Delete all data under the Users collection
- Db.users.remove ({"Name": "LECAF"}) deletes data NAME=LECAF under the Users collection
- Db.users.drop () or Db.runcommand ({"Drop", "users"}) Delete collection users
- Db.runcommand ({"Dropdatabase": 1}) Delete the current database
4. Find
- Db.users.find () Find all data in the Users collection
- Db.users.findOne () to find the first data in the Users collection
5. Modifications
- Db.users.update ({"Name": "LECAF"}, {"Age": ten}) modifies the NAME=LECAF data to age=10, the first parameter is the lookup condition, the second parameter is the modified content, except the primary key, Other content will be replaced by the contents of the second parameter, the primary key cannot be modified,
Third, advanced applications
1. Condition Lookup
- Db.collection.find ({"Key": value}) find data for Key=value
- Db.collection.find ({"Key": {$gt: value} }) key > value
- Db.collection.find ({"key": { $lt: Value}}) key < value
- Db.collection.find ({"key": {$gte: Value}}) key >= value
- Db.collection.find ({"key": {$lte: Value}}) key <= value
- Db.collection.find ({"key": {$gt: value1, $lt: value2}}) value1 < key <value2
- Db.collection.find ({"key": {$ne: Value}}) key <> value
- Db.collection.find ({"key": {$mod: [1]}}) modulo operation, equal to key 10 = = 1 i.e. key divided by 10 + 1
- Db.collection.find ({"key": {$nin: [1, 2, 3]}) does not belong, the condition equal to the value of key does not belong to any of [1, 2, 3]
- Db.collection.find ({"key": {$in: [1, 2, 3]}), the condition equals key equal to either [1, 2, 3]
- Db.collection.find ({"key": {$size: 1}}) $size number, size, condition equal to the number of values of key is 1 (key must be an array, a value can not be counted as an array of 1)
- Db.collection.find ({"key": {$exists: True|false}}) $exists field exists,true returns data with field key, FALSE returns data that does not exist for Word key
- Db.collection.find ({"Key":/^val.*val$/i}) regular, like; "I" Ignores case, "M" supports multiple lines
- Db.collection.find ({$or: [{a:1}, {b:2}]}) $or or (note: MongoDB 1.5.3 later versions are available), the data that meets the criteria a=1 or the criteria b=2 will be queried /c7>
- Db.collection.find ({"Key": Value, $or: [{a:1}, {b:2}]}) meets the criteria Key=value and matches any of the other two criteria
- Db.collection.find ({"Key.subkey": Value}) The values in the inline object match, note: "Key.subkey" must be quoted
- Db.collection.find ({"key": {$not:/^val.*val$/i}}) this is an operator that is used in combination with other query conditions and is not used alone. The result set obtained by the above query condition plus $not can get the opposite set.
2. Sorting
- Db.collection.find (). Sort ({"Key1":-1, "Key2": 1}) here The 1 stands for ascending,-1 for descending
3. Other
- Db.collection.find (). Limit (5) control returns the number of results, if the parameter is 0, then as no constraint, limit () will not work
- Db.collection.find (). Skip (5) controls how much the returned result skips, and if the argument is 0, then skip () will not work, or skip 0
- Db.collection.find (). Skip (5). Limit (5) can be used for paging, skipping 5 data and then fetching 5 data
- Db.collection.find (). Count (True) count () returns the number of result set bars
- Db.collection.find (). Skip (5). Limit (5). Count (True) to obtain the actual number of results returned when adding skip () and limit (), a parameter of true is required. Otherwise, the total number of results that match the query criteria is returned
Windows MongoDB installation and use collation