1. Download and install 64-bit: mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msidownloads.mongodb.comwin32mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi? _ Ga1.242525191.607472782.141145202632 bit: mongodb-win32-i386-2.6.5
1. Download installation 64-bit: mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi http://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi? _ Ga = 1.242525191.607472782.1411452026 32-bit: mongodb-win32-i386-2.6.5
1. Download and install
64-bit: mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi
Http://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi? _ Ga = 1.242525191.607472782.1411452026
32-bit: mongodb-win32-i386-2.6.5.zip
Https://fastdl.mongodb.org/win32/mongodb-win32-i386-2.6.5.zip? _ Ga = 1.181732967.1708362836.1411364634
2. installation directory:
Install the application in this directory:
D: \ MongoDB \
3. Create a directory
D: \ MongoDB \ data \ db
D: \ MongoDB \ data \ log
4. Start the city:
Cd D: \ MongoDB \ bin
Mongod-dbpath "D: \ MongoDB \ data \ db"
5. Test the connection
Open a new window, enter the BINLOG of mongodband cmd.exe. The following information indicates that the test is successful. Now we have entered the test database. Next we will explain how to enter other databases.
Enter exit or ctrl + C to exit.
6.when mongod.exeis closed, mongo.exe cannot be connected to the Data Warehouse. Every time you want to use mongodbdata warehouse to open mongod.exe, It is troublesome. In this case, you can install MongoDB as a windows service.
Run cmd, enter the bin folder, and execute the following command
Run the following command on the console: 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 created and -- serviceName "MongoDB" is named MongoDB.
Start the mongodb service.
> D: \ MongoDB \ bin> net start MongoDB
Open the task manager and you can see that the process has been started.
7. Disable services and delete Processes
> D: \ MongoDB \ bin> NET stop MongoDB (disable Service)
> D: \ MongoDB \ bin> mongod -- dbpath "D: \ MongoDB \ data \ db" -- logpath "D: \ MongoDB \ data \ log \ MongoDB. log "-- remove -- serviceName" MongoDB"
(Delete, note that it is not -- install)
Ii. Use mongodb
1. Common commands
Show dbs: displays the Database List
Use dbname to access the dbname database. It is case sensitive and does not matter if it is not used.
Show collections shows the set in the database, which is equivalent to the table
2. Create & add
Db. users. save ({"name": "lecaf"}) creates a set named users and adds a data entry {"name": "lecaf "}.
Db. users. insert ({"name": "ghost", "age": 10}) inserts a new data in the users set. If there is no users set, mongodb will automatically create
There are also some differences between save () and insert (): If the new data primary key already exists, insert () will not perform the operation and an error will be prompted, while save () the original content is changed to the new content.
Data exists: {_ id: 1, "name": "n1"}, and _ id is the primary key.
Insert ({_ id: 1, "name": "n2"}) will prompt an error
Save ({_ id: 1, "name": "n2"}) changes n1 to n2, which can be updated.
3. Delete
Db. users. remove () delete all data in the users set
Db. users. remove ({"name": "lecaf"}) Delete the data of name = lecaf in the users set
Db. users. drop () or db. runCommand ({"drop", "users"}) deletes the set users
Db. runCommand ({"dropDatabase": 1}) Delete the current database
4. Search
Db. users. find () to find all data in the users set
Db. users. findOne () finds the first data in the users set.
5. Modify
Db. users. update ({"name": "lecaf" },{ "age": 10}) Change name = lecaf's data to age = 10. The first parameter is the search condition, the second parameter is the modification content. Except for the primary key, other content will be replaced by the content of the second parameter. The primary key cannot be modified,
3. Advanced Applications
1. Conditional search
Db. collection. find ({"key": value}) to find data with 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
Db. collection. find ({"key": {$ ne: value}) key <> value
Db. collection. find ({"key": {$ mod: [10, 1]}) modulo operation. The condition is equivalent to key % 10 = 1, that is, the key is divided by the remainder of 10 to 1.
Db. collection. find ({"key": {$ nin: [1, 2, 3]}) does not belong to, and the condition is equivalent to the value of key does not belong to [1, 2, 3] Any
Db. collection. find ({"key": {$ in: [1, 2, 3]}) belongs to, and the condition is equivalent to any one of [1, 2, 3 ].
Db. collection. find ({"key": {$ size: 1}) $ size quantity and size. The number of conditions equivalent to the key value is 1 (The key must be an array, A value cannot be regarded as an array with a quantity of 1)
Db. collection. find ({"key": {$ exists: true | false}) $ exists, true returns data with a field key, and false returns data with no degree key
Db. collection. find ({"key":/^ val. * val $/I}) regular, similar to like; "I" ignores case sensitivity, "m" supports multiple rows
Db. collection. find ({$ or: [{a: 1}, {B: 2}]}) $ or (Note: MongoDB 1.5.3 and later versions are available ), data that meets the condition a = 1 or B = 2 will be queried.
Db. collection. find ({"key": value, $ or: [{a: 1 },{ B: 2}]}) meets the condition key = value, data that meets either of the other two conditions
Db. collection. find ({"key. subkey": value}) value matching in embedded objects. Note: "key. subkey" must be enclosed by quotation marks.
Db. collection. find ({"key": {$ not:/^ val. * val $/I} is an operator used in combination with other query conditions and is not used independently. The opposite set can be obtained after the result set obtained by the preceding query condition is added with $ not.
2. Sorting
Db. collection. find (). sort ({"key1":-1, "key2": 1}) here 1 stands for ascending, and-1 stands for descending
3. Miscellaneous
Db. collection. find (). limit (5) controls the number of returned results. If the parameter is 0, it is treated as no constraint and limit () does not work.
Db. collection. find (). skip (5) controls the number of skipped results returned. If the parameter is 0, it is treated as no constraint. skip () does not work, or 0 is skipped.
Db. collection. find (). skip (5). limit (5) can be used as paging. skip 5 data entries and then retrieve 5 data entries.
Db. collection. find (). count (true) count () returns the number of results set
Db. collection. find (). skip (5 ). limit (5 ). count (true) when the skip () and limit () operations are added, a true parameter is required to obtain the actual number of returned results. Otherwise, the total number of results meeting the query conditions is returned.