Java Operation MongoDB Database

Source: Internet
Author: User
Tags mongodb driver


1, how to start the MongoDB database?


: After installing MongoDB, the folder appears as follows:



MongoDB opens 27017 port by default, open browser, enter http://localhost:27017/, display as follows:




To start a database step:

(1) CD D:\beiyongkaifa\mongodb\bin

(2) Input:mongod--dbpath=d:\beiyongkaifa\mongodb\db, enter. 1 is shown below:


(3) Start another cmd,cd D:\beiyongkaifa\mongodb\bin

(4) Input:MONGO, enter, then the two cmd are shown as follows:



How do I prove that the database is started?

You can open a browser, enter http://localhost:27017/, and display the following: proof that the database has been started.


2. The required MongoDB driver download and import in Java program:

as follows: http://download.csdn.net/detail/jjavaboy/7199317 download Down is a jar package.

The steps to import an external jar package in IDE MyEclipse are: http://jingyan.baidu.com/article/72ee561aba4e2ce16138df00.html


After importing the jar package IDE:





A small demo is as follows:

Import Java.net.unknownhostexception;import Com.mongodb.basicdbobject;import Com.mongodb.db;import Com.mongodb.dbcollection;import Com.mongodb.dbcursor;import Com.mongodb.dbobject;import Com.mongodb.Mongo;import Com.mongodb.mongoexception;public class Mongodb_test {public static void main (string[] args) {try {//Instantiate MONGO object, Connect 27017 port MONGO MONGO = new MONGO ("localhost", 27017);//Connect a database named Yourdb, if the database does not exist, MongoDB will automatically establish DB db = Mongo.getdb (" Yourdb ");//Get collection from MongoDB, database named" Yourdb "//Get a data collection named Yourcolleection from MongoDB, if the data collection does not exist, MongoDB will build Dbcollection collection = Db.getcollection ("Yourcollection") for its new;//Use Basicdbobject object to create a MongoDB document , and given 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 Collection.insert (document);//create Documentbasicdbobject to query searchQuery = new Basicdbobject (); Searchquery.put ("id", 1001);//Use the Find method of collection to find documentdbcursor cursor = collEction.find (searchQuery);//Loop output while (Cursor.hasnext ()) {System.out.println (Cursor.next ());} System.out.println ("Done");} catch (Unknownhostexception e) {e.printstacktrace ();} catch (Mongoexception e) {e.printstacktrace ()}}}

in the cmd window of MongoDB, execute the following command:

(1) Show DBS see which databases are currently available

(2) Use YOURDB using a database

(3) Show collections See what collection are in this database (table)

(4) Db.yourCollection.find () View yourcollection data stored in this table


Results:






Java Operation MongoDB Database

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.