MongoDB3.2.7 JAVA带验证的数据库操作

数据库驱动:mongo-java-driver-3.2.2.jar

JAVA代码:

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;


public class MongoUtil {
	 
	 private static MongoClient mongoClient;
	 private static MongoDatabase mongoDatabase;
	 
	
	public MongoUtil(String dbName) {
		// TODO Auto-generated constructor stub
		ServerAddress serverAddress = new ServerAddress("localhost",27017);  
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();  
        addrs.add(serverAddress);  
        //下面这句,是哪个库的用户,必须在其下面认证才行,我这个用户是root最高权限,所以在要admin下认证,这个地方折腾了好久:(
        MongoCredential credential = MongoCredential.createScramSha1Credential("dy", "admin", "123".toCharArray());  
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
        credentials.add(credential); 
        
        mongoClient = new MongoClient(addrs,credentials);
        mongoDatabase = mongoClient.getDatabase(dbName); 
	}
    /**
     * 创建一个数据库集合
     * @param collName 集合名称
	 * @param db  数据库实例
     */
	public void createCollection(String collName){
		mongoDatabase.createCollection(collName);
	}
	/**
	 * 为相应的集合添加一条或多条数据
	 * @param dbs
	 * @param collName
	 */
	public void insert(List<Document> dbs,String collName){
		//1.得到集合
		MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
		//2.插入操作
		collection.insertMany(dbs);
	}
	/**
	 * 检索某集合中所有的记录
	 * @param collName
	 * @return MongoCursor<Document>
	 */
	public MongoCursor<Document> findAll(String collName){
		MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
		FindIterable<Document> findIterable = collection.find();  
        MongoCursor<Document> mongoCursor = findIterable.iterator();  
        while(mongoCursor.hasNext()){  
           System.out.println(mongoCursor.next());  
        }  
		return mongoCursor;
	}
	/**
	 * 修改某集合中的记录
	 * @param collName
	 * @param whereName
	 * @param where
	 * @return MongoCursor<Document>
	 */
	public void updata(String collName,String whereName,String where,Document dbs){
		MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
		collection.updateMany(Filters.eq(whereName, where), new Document("$set", dbs));  
	}
	/**
	 * 删除某集合中的第一条符合条件的记录
	 * @param  collName
	 * @param  whereName
	 * @param  where
	 */
	public void  delete(String collName,String whereName,String where){
		MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
		collection.deleteOne(Filters.eq(whereName, where));  
	}
	/**
	 *  删除某集合中的全部符合条件的记录
	 *  @param  collName
	 * @param  whereName
	 * @param  where
	 */
	public void  deleteAll(String collName,String whereName,String where){
		MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
		collection.deleteMany (Filters.eq(whereName, where));  
	}
	 public static void main(String[] args) {  
		 MongoUtil mongoDb = new MongoUtil("text1");
		 
		 // 创建一个数据库集合
		// mongoDb.createCollection("bbb");
		 
		 //为相应的集合添加一条或多条数据
//		 Document document = new Document("name", "uuuuu");
//		 List<Document> documents = new ArrayList<Document>();  
//       documents.add(document);  
//		 mongoDb.insert(documents, "aaa");
		 
		 //检索某集合中所有的记录
		 mongoDb.findAll("aaa");
		 //修改某集合中的记录
		 Document document = new Document("name", "uuuuu");
		 mongoDb.updata("aaa","name","aaaa",document);
		 // 删除某集合中的第一条符合条件的记录
//		 mongoDb.delete("aaa","name","uuuuu");
		 //删除某集合中的全部符合条件的记录
//		 mongoDb.deleteAll("aaa","name","uuuuu");
		 
		 //关闭数据库连接
		 mongoClient.close();
	 }
}



测试通过。但是我感觉,下面这句是否会因为查询的数据类型不一样而不能通用?这个暂时还没有测试行不行,如果不行,那通用性就不是很强了。不知道还有没有更好的方法,盼高人指点。
Filters.eq(whereName, where)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值