public static void main(String[] args) {
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的洞口网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
MongoClientOptions.Builder build = new MongoClientOptions.Builder();
// 与数据最大连接数50
build.connectionsPerHost(50);
// 如果当前所有的connection都在使用中,则每个connection上可以有50个线程排队等待
build.threadsAllowedToBlockForConnectionMultiplier(50);
build.connectTimeout(1 * 60 * 1000);
build.maxWaitTime(2 * 60 * 1000);
MongoClientOptions options = build.build();
String host="192.168.80.100";
int port=27017;
MongoClient client = new MongoClient(new ServerAddress(host, port), options);
// 获取数据库test,不存在的话,会自动建立该数据库
MongoDatabase db = client.getDatabase("fanyin");
// { storageEngine: {
// wiredTiger: { configString: 'block_compressor=zlib' }}
CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions();
BasicDBObject configString = new BasicDBObject();
configString.put("configString", "block_compressor=zlib");
BasicDBObject bson = new BasicDBObject();
bson.put("wiredTiger", configString);
createCollectionOptions.storageEngineOptions(bson);
boolean isCollectionExist=collectionExists(db, "data");
if(!isCollectionExist){
db.createCollection("data", createCollectionOptions);
}
MongoCollection
for (int i = 0; i < 1; i++) {
Document document = new Document();
document.append("address2", "sichuan chengdu444444444444444444444444");
document.append("address3", "sichuan chengdu44455555555555555555555555555555");
users.insertOne(document);
}
MongoCollection
for (int i = 0; i < 1; i++) {
Document document = new Document();
document.append("address2", "sichuan chengdu444444444444444444444444");
document.append("address3", "sichuan chengdu44455555555555555555555555555555");
users1.insertOne(document);
}
// MongoClient使用完后必须要close释放资源
client.close();
System.out.println("ooooooooooooookkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
}
//判断collection是否存在
private static boolean collectionExists(MongoDatabase database,String collectionName){
final MongoIterable
try (final MongoCursor
while (it.hasNext()) {
if (it.next().equalsIgnoreCase(collectionName)) {
return true;
}
}
}
return false;
}