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