Download, install, and use MongoDB.

Source: Internet
Author: User
Tags install mongodb

Download, install, and use MongoDB.

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 <value2

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.

 


How to install mongoDB as a system service

The command used is as follows:
Microsoft Windows XP [version 5.1.2600] (C) Copyright: 1985-2001 Microsoft Corp. c: \ Documents and Settings \ Administrator> mongod-dbpath C: \ mongo \ MongoDB \ mongo \ data -- logpath = C: \ mongo \ MongoDB \ mongo \ logs \ mongodb. log -- auth -- installall output going to: C: \ mongo \ MongoDB \ mongo \ logs \ mongodb. logC: \ Documents ents and Settings \ Administrator>
But cannot connect:
C: \ Documents ents and Settings \ Administrator> mongo MongoDB shell version: 2.0.1connecting to: testTue May 29 18:07:50 Error: couldn't connect to server 127.0.0.1 shell/mongo. js: 84 exception: connect failed
After reading the log, you also need to use the net start MongoDB command to start the service. The log is as follows:
Creating service MongoDB. service creation successful. service can be started from the command line via 'net start "MongoDB "'. tue May 29 18:06:43 dbexit: Tue May 29 18:06:43 shutdown: going to close listening sockets... tue May 29 18:06:43 shutdown: going to flush diaglog... tue May 29 18:06:43 shutdown: going to close sockets... tue May 29 18:06:43 shutdown: waiting for fs preallocator... tue May 29 18:06:43 shutdown: closing all files... tue May 29 18:06:43 closeAllFiles () finishedTue May 29 18:06:43 dbexit: really exiting now
Follow the prompts to run net start MongoDB in CMD
Net start "MongoDB" 'C: \ Documents and Settings \ Administrator> net start mongoDB System Error 2. The system cannot find the specified file.
Through the control panel-> Administrative Tools-> services, you can view the services you just installed:
Somehow, the path of the mongod command is changed to C: \ Documents and Settings \ Administrator \, as shown below:
"C: \ ...... the remaining full text>

How to install the java driver of mongodb? mongodb has been installed and the java driver has been downloaded. I don't know what to do next,

Install: mongod -- dbpath "E: \ tool \ db \ mongo \ dbfile" -- logpath "E: \ tool \ db \ mongo \ dbfile \ logs.txt" -- install -- serviceName "MongoDB"
Uninstall: mongod -- remove -- serviceName "MongoDB"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.