First contact with MongoDB reference & paste: http://jingyan.baidu.com/article/ed15cb1b52b8661be2698162.html
One. Installation
1. First go to the official website to download
Go to all versions and choose a slightly less new version-because before looking at the new version there are a variety of uncomfortable
Select a mongodb-win32-x86_64-2008plus-ssl-3.0.14-signed.msi to download
When the download is complete, place the installation package in the D:/MONGODB directory and click Install
Installation Complete:
After the installation is complete, this looks like the MongoDB directory.
2. 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
3.cmd.exe into the DOS interface---Start starting with the specified data path for MongoDB first boot
Perform the following commands:
1 D: 2 3 CD Mongodb\bin 4 5 Mongod.exe--dbpath=d:\mongodb\data\db
View Code
See the information as above on behalf of MongoDB first success, the default MongoDB port number is 27017 similar to MySQL port number is 33,061 kind. As follows: Can be seen at the end
4. Test the connection and exit
Restart a 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.
Exit
5. Next we will install the MONGODB bit Windows service
When Mongod.exe is closed, Mongo.exe cannot connect to the database, so it is troublesome to open the Mongod.exe program every time you want to use the MongoDB database, so 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
6. Start a service named MongoDB
> D:\mongodb\bin>net START MongoDB
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)
Two. Simple to use
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": 10}) Inserts a new piece of 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": 10})
Modify NAME=LECAF data is age=10, the first parameter is a lookup condition, the second parameter is the modification, in addition to the primary key, the other content will be replaced by the contents of the second parameter, the primary key cannot be modified
6. 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: [10, 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 equals the value of key does not belong to either [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 with no 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), eligible a=1 or eligible b=2 data will be queried
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.
7. Sorting
Db.collection.find (). Sort ({"Key1":-1, "Key2": 1}) Here the 1 stands for ascending,-1 for descending
8. Other
Db.collection.find (). Limit (5) controls the number of returned results, and if the argument is 0, the limit () will not work if it is not constrained
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
"MongoDB" 1. Installation--and easy to use