初始化mongo链接
MongoClientOptions options = new Builder().connectionsPerHost(300).connectTimeout(20000)
.maxConnectionIdleTime(60000).maxWaitTime(1000 * 60 * 2).socketKeepAlive(true).socketTimeout(60000)
.threadsAllowedToBlockForConnectionMultiplier(50).build();
MongoCredential credential = MongoCredential.createCredential("foest", "markett",
"123456".toCharArray());
ServerAddress serverAddress = new ServerAddress("192.1.15.19", 20000);
MongoClient mongoClient = new MongoClient(serverAddress, credential, options);
database = mongoClient.getDatabase("market-data-forex-test");
kLineCollection = database.getCollection("KLine");
封装查询条件:
BasicDBObject query = new BasicDBObject();
query.append("code", "CU00");
query.append("period", 1);
query.put("barTime", new BasicDBObject("$gte", 20171127210100L).append("$lt", 20171127210200L));
query.append("code", "CU00");
query.append("period", 1);
query.put("barTime", new BasicDBObject("$gte", 20171127210100L).append("$lt", 20171127210200L));
//查询该条件下数据的数量
long klineAmount = kLineCollection.countDocuments(query);
来源:https://www.cnblogs.com/redBack/p/10020327.html