MongoDB项目中常用方法

使用MongoDB连接池MongoOptions来进行连接 以及相关方法的调用

//获得驱动地址(这里的驱动 写入了配置文件中)
String serverAddressStr = Configure.getInstance().getProperty("SERVER_ADDRESSES");
			log.debug("serverAddressStr:" + serverAddressStr);

如果需要连接的MongoDB为多个,则用逗号分隔开,加入集合中以便后续使用
String[] serverAddressArray = serverAddressStr.split(",");
			for (String address : serverAddressArray) {
				log.debug("address:" + address);
				addresslist.add(new ServerAddress(address));
			}
//声明MongoOptions对象
MongoOptions options = new MongoOptions();
//autoConnectRetry方法用于在连接失败后是否重新连接,可写成配置项
		String autoConnectRetry = Configure.getInstance().getProperty("AUTO_CONNECT_RETRY");
if (StringUtils.isNotEmpty(autoConnectRetry)) {
			options.autoConnectRetry = Boolean.valueOf(autoConnectRetry);
		}
//设置连接池的大小也可写成配置项 方便以后调整 使用的是autoConnectRetry方法
		String connectionsPerHost = Configure.getInstance().getProperty("CONNECTIONS_PER_HOST");
if (StringUtils.isNotEmpty(connectionsPerHost)) {
			options.connectionsPerHost = Integer.valueOf(connectionsPerHost);
		}
//线程队列
String threadsAllowedToBlockForConnectionMultiplier = Configure.getInstance().getProperty("THREAD_ALLOWED");
		log.debug("[THREAD_ALLOWED]:" + threadsAllowedToBlockForConnectionMultiplier);
		if (StringUtils.isNotEmpty(threadsAllowedToBlockForConnectionMultiplier)) {
			options.threadsAllowedToBlockForConnectionMultiplier = Integer.valueOf(threadsAllowedToBlockForConnectionMultiplier);
		}
//最大阻塞时间
String connectTimeout = Configure.getInstance().getProperty("CONNECT_TIME_OUT");
		log.debug("[CONNECT_TIME_OUT]:" + connectTimeout);
		if (StringUtils.isNotEmpty(connectTimeout)) {
			options.connectTimeout = Integer.valueOf(connectTimeout);
		}
// 被阻塞线程从连接池获取连接的最长等待时间(ms)
		// options.maxWaitTime = 12000;
		String maxWaitTime = Configure.getInstance().getProperty("MAX_WAIT_TIME");
		log.debug("[MAX_WAIT_TIME]:" + maxWaitTime);
		if (StringUtils.isNotEmpty(maxWaitTime)) {
			options.maxWaitTime = Integer.valueOf(maxWaitTime);
		}
// 是否答应驱动从次要节点读取数据,默认为false
		String slaveOk = Configure.getInstance().getProperty("SlAVE_OK");
		log.debug("[SlAVE_OK]:" + slaveOk);
		if (StringUtils.isNotEmpty(slaveOk)) {
			options.slaveOk = Boolean.valueOf(slaveOk);
		}

 从某个中按照字段查找相应数据 并放入集合中

public DBObject findOne(String collectionName,String keystr,String value){
    		DB db = this.getDB();
		DBCollection collection = db.getCollection(collectionName);
		DBObject dbObject;
		try {
			dbObject = collection.findOne(new BasicDBObject(keystr, value));
			log.debug("dbObject1:" + dbObject);
			if (dbObject == null) {
				db = this.switchCluster().getDB();
				collection = db.getCollection(collectionName);
				dbObject = collection.findOne(new BasicDBObject(keystr, value));
				log.debug("dbObject1-2:" + dbObject);
			}
		} catch (MongoException e) {
			db = this.switchCluster().getDB();
			collection = db.getCollection(collectionName);
			dbObject = collection.findOne(new BasicDBObject(keystr, value));
			log.debug("dbObject2:" + dbObject);
		}
		return dbObject;
	}

  前台通过DBCollection 根据名称获取相应的value 然后加入list中~

 	public List<DBObject> getValue(List<String> columnNames) {
             
  db = MongoDB.getInstance().getDB();
		DBCollection collection = db.getCollection("labels");
		List<DBObject> list = new ArrayList<DBObject>();
		BasicDBList dbList = new BasicDBList();
		dbList.addAll(columnNames);
		DBObject inObj = new BasicDBObject("$in", dbList);
		DBCursor cursor = collection.find(new BasicDBObject("column_name", inObj));
		DBObject dbObj = null;
		while (cursor.hasNext()) {
			dbObj = cursor.next();
			list.add(dbObj);
		}
		return list;                        
posted on 2015-10-29 13:08  松伯  阅读(576)  评论(0编辑  收藏  举报