MongoDB Installation (window) and Java Connectivity Test

Source: Internet
Author: User
Tags mongodb
Download Installation First

MongoDB website Download Select window version



The second step is to install

Note The installation directory, my installation directory is D:\Program Files (x86) \mongodb


Step three: Start

Open the cmd window and enter the carriage: D:

Enter and then enter: CD D:\Program Files (x86) \mongodb\server\3.0\bin

Continuation: Mongod-dbpath "D:\Program Files (x86) \mongodb\server\3.0\data\db"

Note: Here is the directory where the specified data is stored, MongoDB's default port number is 27017, the browser input 127.0.0.1:27017 prompted to start successful, close the cmd window will exit the program

New Open a CMD window input and then enter: D:

Enter and then enter: CD D:\Program Files (x86) \mongodb\server\3.0\bin

Enter and then enter: MONGO

This is the command line interface for the database,

Enter exit or CTRL + C to exit.

When the Mongod.exe is closed, Mongo.exe cannot connect to the database, so every time you want to use the MongoDB database to open the Mongod.exe program, so more trouble, we install the window service


Step Fourth: Install as a service


Continue typing in cmd: Mongod--dbpath "D:\Program files (x86) \mongodb\server\3.0\data\db"--logpath "D:\Program Files (x86) \mongodb \server\3.0\data\log\mongodb.log "--install--servicename" MongoDB so that a service named MongoDB would appear in the window service, it would be much more convenient
here is my test with Java connection MongoDB The driver package that references MongoDB

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 Mon

		Go ("localhost", 27017);

		Connect the database named Yourdb, if the database does not exist, MongoDB will automatically establish DB db = Mongo.getdb ("mytest");
		Iterate through the names of all sets set<string> Colls = Db.getcollectionnames ();
		SYSTEM.OUT.PRINTLN ("All sets:");
		for (String s:colls) {System.out.println (s);
		} System.out.println ("Test start"); The data collection named Yourcolleection is obtained from the MongoDB, and if the data collection does not exist, MongoDB creates a new dbcollection collection = Db.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 MongoDB document and assign a value.
		Basicdbobject document = new Basicdbobject (); Document.pUT ("id", 1001);
		Document.put ("msg", "Hello World MongoDB in Java");
	Save the newly established document in collection 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 document Basicdbobject SearchQuery = new Basicdbobject () to query;
		Searchquery.put ("id", 1001);
		Finds document dbcursor cursor = Collection.find () using the collection Find method.
		Loop output while (Cursor.hasnext ()) {System.out.println (Cursor.next ()); }
	}
}


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.