Java Operation MySQL Database code we have a very familiar, and additions and deletions, Java to the MongoDB database is similar to the operation, first database connection, and then the operation.
First we enter into the admin database, and then set up their own database Testmongodb, access to the admin database, you can directly into the TESTMONGODB, because the user can enter the system database, is super Administrator, use Testmongodb, Set the user name and password for the database, Db.adduser (' root ', ' root ') so that we can connect to the database in the program and implement additions and deletions, as shown in the code below.
The code looks like this:
<pre name= "code" class= "java" >package com.mkyong.core;import Java.net.unknownhostexception;import Java.util.date;import Com.mongodb.basicdbobject;import Com.mongodb.db;import Com.mongodb.dbcollection;import Com.mongodb.dbcursor;import Com.mongodb.mongo;import com.mongodb.mongoexception;/** * Java + mongodb Hello World Example * */public class App {public static void main (string[] args) {try {/**** Connect to MongoDB ****///Since 2.10.0, Uses mongoclient//mongoclient MONGO = new Mongoclient ("localhost", 27017); Mongo Mongo = new Mongo ("127.0.0.1", 27017); /**** Get Database ****///If database doesn ' t exists, MongoDB would create it for youdb db = Mongo.getdb ("Testmongodb");// Database username root and password root boolean ok = db.authenticate ("root", "root". ToCharArray ()); if (OK) {SYSTEM.OUT.PR INTLN ("DB connection success! ");} {System.out.println ("DB connection Fail! ");} /**** Get collection/table from ' Testmongodb ' ****///if collection doesn ' t exists, MongoDB would creaTe it for youdbcollection table = db.getcollection ("user"),/**** Insert ****///Create a document to store key and Valueba Sicdbobject document = new Basicdbobject ();d ocument.put ("name", "Mkyong");d ocument.put ("Age", "a");d Ocument.put (" CreatedDate ", New Date ()); Table.insert (document);/**** Find and display ****/basicdbobject searchQuery = new Basicdbobject (); Searchquery.put ("Name", "Mkyong");D bcursor cursor = table.find (searchQuery); while (Cursor.hasnext () ) {System.out.println (Cursor.next ());} /**** Update ****///Search document where name= "Mkyong" and Update it with new valuesbasicdbobject query = new Basicdbobj ECT (); Query.put ("Name", "Mkyong"); Basicdbobject newdocument = new Basicdbobject () newdocument.put ("name", "mkyong-updated"); Basicdbobject updateobj = new Basicdbobject () updateobj.put ("$set", newdocument); table.update (query, updateobj);/*** * Find and display ****/basicdbobject SearchQuery2 = new Basicdbobject (). Append ("name", "mkyong-updated");D Bcursor Cursor2 = Table.find (searchQuery2); while (Cursor2.hasnext ()) {System.out.println (Cursor2.next ());} /**** done ****/system.out.println ("Done");} catch (Unknownhostexception e) {e.printstacktrace ();} catch (Mongoexception e) {e.printstacktrace ()}}}
The console input results are as follows:
DB connection success!
DB connection Fail!
{"_id": {"$oid": "544073c4d58dfa6e469555ba"}, "name": "Mkyong", "age": +, "CreatedDate": {"$date": "2014-10-1 7t01:41:24.479z "}}
{"_id": {"$oid": "543e154bd58d704982fd38f0"}, "name": "Mkyong-updated", "age": +, "CreatedDate": {"$date": "2 014-10-15t06:33:47.321z "}}
{"_id": {"$oid": "5440719dd58d08a207605c8e"}, "name": "Mkyong-updated", "age": +, "CreatedDate": {"$date": "2 014-10-17t01:32:13.922z "}}
{"_id": {"$oid": "544073c4d58dfa6e469555ba"}, "name": "Mkyong-updated", "age": +, "CreatedDate": {"$date": "2 014-10-17t01:41:24.479z "}}
Done
SOURCE Download: http://download.csdn.net/detail/lovesummerforever/8047875
Easy to find MongoDB (vi) Java Operation MongoDB Additions and deletions