Java uses mongoDB

Source: Internet
Author: User
Tags install mongodb website server

My computer environment: win7 64-bit System

1. Install mongoDB

Go to the official website to download the http://www.mongodb.org/downloads to choose the version suitable for your system.

After downloading and decompress the package, I decompress the package on the D Drive, even if it is installed. As follows:

How to start it?

How to store data and logs first? Look at my above.

Then start.

Command line:

D: \ mongodb \ bin \ mongod.exe-dbpath = d: \ mongodb \ data

D: \ mongodb \ bin \ mongod.exe-logpath = d: \ mongodb \ logs

Every time it starts like this, it is very troublesome to put the two lines of code into the txt file, and then change the suffix to bat, such as my run. bat, which I created myself.

Note: The startup may fail because, if you close the command line interface abnormally, mongodb will create a. lock file and delete it. You can optimize the bat file so that you do not need to manually delete it later.

2. graphical mongodb management?

The tool I selected is rockmongodb, which is a mongod management tool written in php. How can I use it?

You need to set up a wamp dynamic website server, which is available on the wampserver,

After installation, copy rockmongodb to the www file directory under wamp.

Start wamp. Note that you can delete index. php under www or change the name. The reason is not explained.

Rockmongodb cannot be used at this time, because the phpmongodb plug-in is required and wampserver is not used by default,

Go to download php_mongodb module, address: http://www.php.net/manual/en/mongo.installation.php

Check your php and vc versions.

Decompress the downloaded file and copy php_mongodb.dll to the ext price in the php (with version number) directory in your wamp bin.

Add the following in the php. ini file:

?

1 Extension = php_cmd.dll

Restart wamp and enable the php_mongodb module in php on the console. If no, the downloaded php_mongodb.dll is incorrect.

You can download multiple versions from github, but remember to copy the file to the ext folder and change the file name to php_mongodb.dll.

Then you can use rockmongo. If you want to use rockmongodb, go to the official rockmongodb website. I will not introduce it here.

3. Link mongodb to java.

Download the mongodb java driver first,

The link provided by the official website is not quite correct.

Accurate: https://github.com/mongodb/mongo-java-driver/downloads

Select a suitable version.

Add the jar file to the path of one of your eclipse projects. You can also add the jar file to the global directory.

Test whether the connection can be connected.

Note: I added a test database under rongmongdb and created a blog set to store a piece of data {name: peng, age: 100}

Java test code:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 Import java.net. unknownHostException; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. DBCursor; import com. mongodb. DBObject; import com. mongodb. consumer client; public class Demo {public static void main (String [] args) throws UnknownHostException {Consumer client = new Consumer client (); DB db = client. getDB ("test"); DBCollection blog = db. getCollection ("blog"); System. out. println (blog. find (); DBCursor cursor = blog. find (); for (DBObject dbObject: cursor) {System. out. println (dbObject );}}}

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.