MongoDB Installation (window) and Java Connection test

Source: Internet
Author: User

Download the 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 input and enter: D:

input and then 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 the specified data is stored, the default port number of MongoDB is 27017, the browser input 127.0.0.1:27017 is prompted to start successfully, close the CMD window will exit the program

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

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

Input and then enter: MONGO

This is the command line interface of the database,

Enter exit or CTRL + C to exit.

when Mongod.exe is closed, Mongo.exe cannot connect to the database, so every time you want to use a MongoDB database, you need to open Mongod.exe Process Order, so the trouble is, we install the window service


Fourth step: Install as a service


Continue in cmd input: 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 "this will cause a service called MongoDB in the window service to be more convenient.
Here is the test I used to connect MongoDB with JavaNote 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 Mongo ("localhost", 27017);//Connect a database named Yourdb, if If the database does not exist, MongoDB will automatically establish DB db = Mongo.getdb ("mytest");//traverse all sets of names set<string> colls = Db.getcollectionnames (); System.out.println ("All Collections:"); for (String s:colls) {System.out.println (s);} System.out.println ("Test Start");//Get a collection of data named Yourcolleection from MongoDB, and if that data collection does not exist, MongoDB will create a new dbcollection for it 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 give the assignment. Basicdbobject document = new Basicdbobject ();d ocument.put ("id", 1001);d ocument.put ("msg", "Hello World MongoDB in Java") ;//Save the newly created document to 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 Documentbasicdbobject to query SearchQuery = new Basicdbobject () searchquery.put ("id", 1001);//Find Documentdbcursor cursor using Collection's Find method = Collection.find ();//Loop output while (Cursor.hasnext ()) {System.out.println (Cursor.next ())}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MongoDB Installation (window) and Java Connection test

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.