MongoDB Learning (2) Add, query, modify, and delete MongoDBJava

Source: Internet
Author: User
Related information 1. MongoDBforJava driver package github. commongodbmongo-java-driverdownloads2, online documentation www. mongodb. orgdisplayDOCSJava + Language + Center operation 1. query all data in a table (called a set in MongoDB) using the Java code DBTest. javapack

Related information 1, MongoDB for Java driver package https://github.com/mongodb/mongo-java-driver/downloads 2, online document http://www.mongodb.org/display/DOCS/Java+Language+Center operations 1, query a table (called set in MongoDB) All data Java code DBTest. java pack

Related Materials

1. MongoDB for Java driver package

Https://github.com/mongodb/mongo-java-driver/downloads

2. Online Documentation

Http://www.mongodb.org/display/DOCS/Java+Language+Center

Operation

1. query all data in a table (called a set in MongoDB ).

Java code DBTest. java

Package com. archie. mongodb;

Import java.net. UnknownHostException;

Import com. mongodb. DB;
Import com. mongodb. DBCollection;
Import com. mongodb. DBCursor;
Import com. mongodb. DBObject;
Import com. mongodb. Mongo;
Import com. mongodb. parse exception;

/**

* Query all data in the specified DBCollection set of the specified database
* @ Author archie2010
*
* Since 10:40:21
*/
Public class DBTest {
Public static void main (String [] args) throws UnknownHostException,
Except exception {
/**
The Mongo instance represents a database connection pool.
* Mongo mg = new Mongo ("localhost ");
Mongo mg = new Mongo ("localhost", 27017 );
*/
Mongo mg = new Mongo ();

// Obtain the database object named "dbtest"
DB db = mg. getDB ("dbtest ");
// Query all sets in the database (equivalent to tables)
For (String name: db. getCollectionNames ()){
System. out. println ("Collection Name:" + name );
}
DBCollection users = db. getCollection ("emp ");
// Query all data in the Set
DBCursor cur = users. find ();
System. out. println ("Record Count:" + cur. count ());
While (cur. hasNext ()){
DBObject object = cur. next ();
System. out. println (object );
// Retrieve the data with the Field Names 'uname' and 'upwd 'in the object list.
System. out. println ("uname:" + object. get ("uname") + "\ tupwd :"
+ Object. get ("upwd "));
}
}
}

Running result:

2. CRUD operation on the specified DBCollection set

Java code

DBUtil. java

Package com. archie. mongodb;

Import java.net. UnknownHostException;

Import com. mongodb. DB;
Import com. mongodb. DBCollection;
Import com. mongodb. Mongo;

/**
* Get the tool class of the DBCollection set
* @ Author archie2010
*
* Since 10:54:42
*/
Public class DBUtil {

Public static Mongo mg = null;
Public static DB db = null;
Public static DBCollection collection;

/**
* Get the DBCollection object
* @ Param dbName
* @ Param colName
* @ Return
*/
Public static DBCollection getDBCollection (String dbName, String colName ){
If (mg = null ){
Try {
Mg = new Mongo ();
} Catch (UnknownHostException e ){
E. printStackTrace ();
}
}
If (db = null ){
Db = mg. getDB (dbName );
}
Return db. getCollection (colName );
}
}

CRUDTest. java

Package com. archie. mongodb;

Import com. mongodb. BasicDBObject;
Import com. mongodb. DBCollection;
Import com. mongodb. DBCursor;
Import com. mongodb. DBObject;

/**

* CRUD operation on the specified DBCollection set
* @ Author archie2010
*
* Since 10:51:24
*/
Public class CRUDTest {
/**
* Added
* @ Param obj
*/
Public static void add (DBObject obj ){
DBCollection coll = DBUtil. getDBCollection ("dbtest", "emp ");
Coll. insert (obj );
}
/**
* Delete
* @ Param obj
*/
Public static void delete (DBObject obj ){
DBCollection coll = DBUtil. getDBCollection ("dbtest", "emp ");
Coll. remove (obj );
}
/**
* Query
*/
Public static void query (){
DBCollection coll = DBUtil. getDBCollection ("dbtest", "emp ");
// Query all data in the Set
DBCursor cur = coll. find ();
System. out. println ("Record Count:" + cur. count ());
While (cur. hasNext ()){
DBObject object = cur. next ();
System. out. println (object );
// Retrieve data whose list is 'uname' and 'upwd'
System. out. println ("uname:" + object. get ("uname") + "\ tupwd :"
+ Object. get ("upwd") + "\ t_id:" + object. get ("_ id "));
}
}
/**
* Modify
*/
Public static void modify (DBObject orig, DBObject update ){
DBCollection coll = DBUtil. getDBCollection ("dbtest", "emp ");
Coll. update (orig, update, true, false );
}
Public static void main (String [] args ){
DBObject empObj = new BasicDBObject ();
EmpObj. put ("uname", "teddy ");
EmpObj. put ("upwd", "123456 ");
// Add
Add (empObj );
Query ();

DBObject updateObj = new BasicDBObject ();
UpdateObj. put ("uname", "teddy ");
UpdateObj. put ("upwd", "3333 ");
// Update
Modify (new BasicDBObject ("uname", "teddy"), updateObj );
System. out. println ("----------------------- after modification -------------------");
Query ();

// Delete
Delete (new BasicDBObject ("uname", "teddy "));
System. out. println ("----------------------- after deletion -------------------");
Query ();
}
}

Running effect:

Sample download

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.