博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB项目中常用方法
阅读量:5046 次
发布时间:2019-06-12

本文共 3595 字,大约阅读时间需要 11 分钟。

使用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
getValue(List
columnNames) { db = MongoDB.getInstance().getDB(); DBCollection collection = db.getCollection("labels"); List
list = new ArrayList
(); 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;

转载于:https://www.cnblogs.com/yangsy0915/p/4920178.html

你可能感兴趣的文章
Redis事务
查看>>
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
CSS兼容性常见问题总结
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>
每天一个小程序—0005题(批量处理图片大小)
查看>>
IIS的各种身份验证详细测试
查看>>
JavaScript特效源码(3、菜单特效)
查看>>
Linux常用命令总结
查看>>
yii模型ar中备忘
查看>>
C#线程入门
查看>>
CSS清除浮动方法
查看>>
JVM内存回收机制简述
查看>>
洛咕 P2480 [SDOI2010]古代猪文
查看>>
js-创建对象的几种方式
查看>>
JDK JRE Java虚拟机的关系
查看>>
2018.11.20
查看>>
word20161215
查看>>
12th week blog
查看>>
dijkstra (模板)
查看>>