Mongodb installation (window) and java connection test, mongodbwindow

Source: Internet
Author: User
Tags install window mongodb driver

Mongodb installation (window) and java connection test, mongodbwindow
First download and install

Download mongodb official website and select window version



Step 2: Install

Pay attention to the installation directory. My installation directory is D: \ Program Files (x86) \ MongoDB


Step 3: Start

Open the cmd window and enter: D:

Enter and press Enter: cd D: \ Program Files (x86) \ MongoDB \ Server \ 3.0 \ bin

Continue: mongod-dbpath "D: \ Program Files (x86) \ MongoDB \ Server \ 3.0 \ data \ db"

Note: Here is the directory where data is stored. The default port number of mongodb is 27017. If you enter 127.0.0.1: 27017 in your browser, a prompt is displayed, indicating that the system is successfully started. Close the cmd window and exit the program.

Enter a new cmd window and press Enter: D:

Enter and press Enter: cd D: \ Program Files (x86) \ MongoDB \ Server \ 3.0 \ bin

Enter and press Enter: mongo

This is the command line interface of the database,

Enter exit or ctrl + C to exit.

When mongod.exeis closed, mongo.exe cannot be connected to the Data Warehouse. Every time I want to use mongodbdata warehouse, I want to open mongod.exe program, so it is very troublesome. we install window service.


Step 4: Install as a service


Enter mongod -- dbpath "D: \ Program Files (x86) \ MongoDB \ Server \ 3.0 \ data \ db" -- logpath "D: \ Program Files (x86) in cmd) \ MongoDB \ Server \ 3.0 \ data \ log \ MongoDB. log "-- install -- serviceName" MongoDB ", a service named MongoDB will appear in the window service, which is much easier.
The following is a test of connecting to mongodb using java. Note that the mongodb driver package is referenced.
Package test1; import java. util. date; import java. util. set; import com. mongodb. basicDBObject; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. DBCursor; import com. mongodb. mongo; public class ConnectionTest {/*** @ param args */public static void main (String [] args) {Mongo mongo = new Mongo ("localhost", 27017 ); // connect to the database named yourdb. If the database does not exist, mongodb automatically creates DB = mongo. getDB ("mytest "); // Traverse all sets named Set <String> colls = db. getCollectionNames (); System. out. println ("All sets:"); for (String s: colls) {System. out. println (s);} System. out. println ("Test start"); // obtain the data set named yourColleection from Mongodb. If the data set does not exist, Mongodb creates a DBCollection = db for the new one. getCollection ("yourCollection"); search (collection); System. out. println ("Done");} public static void insert (DBCollection collection) {System. out. println ("insert "); // Use the BasicDBObject object to create a document for mongodb and assign a value to it. BasicDBObject document = new BasicDBObject (); document. put ("id", 1001); document. put ("msg", "hello world mongoDB in Java"); // Save the newly created document to collection. insert (document);} public static void delete (DBCollection collection) {System. out. println ("delete"); BasicDBObject query = new BasicDBObject (); query. put ("id", 1001); collection. remove (query);} public static void update (DBCollection collection) {System. out. println ("update"); BasicDBObject update = new BasicDBObject (); update. put ("id", 1002); update. put ("time", new Date (); collection. update (new BasicDBObject (). append ("id", 1001), update);} public static void search (DBCollection collection) {System. out. println ("query"); // create the documentBasicDBObject searchQuery = new BasicDBObject (); searchQuery. put ("id", 1001); // use the find method of collection to find documentDBCursor cursor = collection. find (); // loop output result while (cursor. hasNext () {System. out. println (cursor. next ());}}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.