Java uses the latest API to manipulate MongoDB

Source: Internet
Author: User
Tags iterable mongoclient

//

Package Com.auto.test.dbmodel;

Import java.util.ArrayList;

Import org.bson.Document;
Import Org.bson.conversions.Bson;

Import com.mongodb.MongoClient;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoDatabase;

/**
* Class Description
*
* @author LUOZT
* @date New September 19, 2016
*/
public class Dbconnutil {



Public String IP;
public int port;
Public String dbname;
Mongoclient mongoclient;
Mongodatabase DB;
Finditerable<document> Cursor=null;
Long update_count=0;
Long delete_count=0;


Public Dbconnutil (String ip,int port,string dbname) {

This.ip=ip;
This.port=port;
This.dbname=dbname;

}


Public Mongodatabase Conn ()
{
Mongoclient = new Mongoclient (IP, port);
db = Mongoclient.getdatabase (dbname);

return DB;

}

/**query Data from Database
*
* @param collection_name
* @param document for query criteria
* @param document_projection to return only the fields you need
* @return
* Crud Method Please see http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/
*/

@SuppressWarnings ("Unchecked")
Public finditerable<document> querydata (String collection_name,bson document,int flag) {
Collection_name is the collection name to query
mongocollection<document> coll = db.getcollection (collection_name);

if (flag==0) {
0 means query All
Cursor=coll.find (document);
}
else if (flag==1) {
1 means querying only one piece of data
Cursor= (finditerable<document>) coll.find (Document). First ();

}

else if (flag==2) {
2 means no query criteria
Cursor=coll.find ();

}
return cursor;
}

@SuppressWarnings ("Unchecked")
Public finditerable<document> querydata (String collection_name,bson document,bson document_projection,int Flag ){

mongocollection<document> coll = db.getcollection (collection_name);
if (flag==0) {
0 means query All
Cursor=coll.find (document). projection (document_projection);
}

else if (flag==1) {
1 means querying only one piece of data
Cursor= (finditerable<document>) coll.find (Document). Projection (document_projection). First ();
}

else if (flag==2) {
2 means no query criteria
Cursor=coll.find (). projection (document_projection);
}
return cursor;
}



/**insert data to database;
*
* Crud Method Please see http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/
*/
public void InsertData (String collection_name,arraylist<document> documents) {


mongocollection<document> coll = db.getcollection (collection_name);

Coll.insertmany (documents);


}


/**
*
* Update data to Database
*
* Filter filters the data that needs to be updated with this filter
* Update what needs to be updated
*
*
*
* Crud Method Please see http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/
*/


Public long UpdateData (String Collection_name,bson filter,bson update,int flag) {


mongocollection<document> coll = db.getcollection (collection_name);
if (flag==0) {
Update_count=coll.updatemany (filter, update). Getmodifiedcount ();
return update_count;
}

else if (flag==1) {

Update_count=coll.updateone (filter, update). Getmodifiedcount ();
return update_count;
}
return update_count;
}




/**
*
* Remove data from database
*
* Filter filters the data to be deleted via this filter
* Crud Method Please see http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/
*/

Public long DeleteData (String collection_name,bson filter,int flag) {

mongocollection<document> coll = db.getcollection (collection_name);
if (flag==0) {

Delete_count=coll.deletemany (filter). Getdeletedcount ();
return delete_count;
}
else if (flag==1) {

Delete_count=coll.deleteone (filter). Getdeletedcount ();
return delete_count;

}

return delete_count;
}


}

Dbunit

Package com.auto.test.testcase;

Import java.util.ArrayList;

Import org.bson.Document;
Import Com.auto.test.basedata.DBConfig;
Import Com.auto.test.dbmodel.DBConnUtil;
Import Com.mongodb.Block;
Import com.mongodb.client.FindIterable;

/**
* Class Description
*
* @author LUOZT
* @date New September 19, 2016
*/
public class Dbunit_test implements dbconfig{


String collection_name= "Test_api";

public static void Main (string[] args) {
TODO auto-generated Method Stub
Dbunit_test test=new dbunit_test ();
}


Public Dbunit_test () {

Dbconnutil dbhelp=new Dbconnutil (mongo_ip,mongo_port,mongo_dbname);
Dbhelp.conn ();


/**
* Insert
* If database or collection not exist, it is created by Automatic
*/

Arraylist<document> documents=new arraylist<document> ();

Document Document=new document ("name", "MONGODB2")
. Append ("type", "Database2")
. Append ("Count", "112")
. Append ("Info", New Document ("X", 2032). Append ("Y", 1022));

Documents.Add (document);

Dbhelp.insertdata (collection_name, documents);



/**
* Query
*
*/
Document Query_document=new document ("Info.x", New Document ("$GT", 300));
Document Fiter_document=new document ("name", 1). Append ("Count", 1);

Finditerable<document> Iterable=dbhelp.querydata (collection_name, query_document, fiter_document, 0);
Iterable.foreach (New block<document> () {
@Override
public void apply (Final Document document) {
System.out.println (document);
}
});



/**
* Update
*
*/

Document Update_document=new document ("name", "MONGODB2");
Document Update_option=new document ("$set", New Document ("Info.y", 2000));
Long Update_count=dbhelp.updatedata (collection_name, update_document, update_option, 0);
System.out.println (Update_count);



/**
*
* Delete
*
*
*/
Document Delete_document=new document ("type", "database3");

Long Delete_count=dbhelp.deletedata (collection_name, delete_document, 0);
System.out.println ("Delete Count is" +delete_count+ "");

}




}

Java uses the latest API to manipulate MongoDB

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.