java 使用最新api操作mongodb

//

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;

/**
* 类说明
*
* @author luozt
* @date 2016年9月19日 新建
*/
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 为查询条件
* @param document_projection为只返回需要的字段
* @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为要查询的collection名字
MongoCollection<Document> coll = db.getCollection(collection_name);

if(flag==0){
//0 代表查询所有
cursor=coll.find(document);
}
else if(flag==1){
//1 代表只查询一条数据
cursor=(FindIterable<Document>) coll.find(document).first();

}

else if(flag==2){
//2 代表没有查询条件
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 代表查询所有
cursor=coll.find(document).projection(document_projection);
}

else if(flag==1){
//1 代表只查询一条数据
cursor=(FindIterable<Document>) coll.find(document).projection(document_projection).first();
}

else if(flag==2){
//2 代表没有查询条件
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 通过此filter过滤需要更新的数据
* update 需要更新的内容
*
*
*
* 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 通过此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;

/**
* 类说明
*
* @author luozt
* @date 2016年9月19日 新建
*/
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 will be 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+””);

}

}

 

    原文作者:MongoDB
    原文地址: https://www.cnblogs.com/luo-mao/p/5888356.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞