mongodbdb启用wiredTiger引擎及zlib压缩
admin
2023-01-26 10:23:41
0



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 users = db.getCollection("data");

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 users1 = db.getCollection("data1");

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 iterable = database.listCollectionNames();

   try (final MongoCursor it = iterable.iterator()) {

       while (it.hasNext()) {

           if (it.next().equalsIgnoreCase(collectionName)) {

               return true;

           }

       }

   }


   return false;

}


相关内容

热门资讯

白宫发布《科学:新的黄金时代》... 据白宫7月21日消息,白宫科技政策办公室、管理与预算办公室联合发布《科学:新的黄金时代》报告。本次备...
民营商业火箭首次从长三角东海海... 北京时间7月22日10时54分,我国太原卫星发射中心在上海东部海域使用引力一号遥四运载火箭,将搭载的...
美威胁袭伊基础设施,伊朗军方:... △当地时间21日,船只在霍尔木兹海峡航行伊朗武装部队哈塔姆安比亚中央总部当地时间22日发布声明称,美...
扑翼机器人问世:像潜水鸟一样在... 潜鸟、海鸥、海鹦和海燕,是自然界中约100种既能飞行又能游泳的鸟类。这些潜水鸟能够俯冲入水追逐猎物,...
当科学家走进西部脑谷:这里有望... 7月22日,2026天府科学家创新发展大会暨成都科产融合对接活动锦江经开区脑机接口专场在锦江区成功举...
女子身份证丢失后名下出现“吸毒... 四川籍的庞女士定居浙江宁波多年,她怎么也没想到,因丢失身份证一事,竟让她陷入长达7年“自证清白”的困...
省农科院成功研发我省首个高粱全... 近日,记者从省农科院获悉,该院作物资源研究所、农业生物技术研究所联合石家庄博瑞迪生物,成功研制出省内...
消息称AMD与Anthropi... IT之家 7 月 22 日消息,据华尔街日报消息,AMD 与 Anthropic 达成了一项价值数百...
独家!苹果iPhone18系列... 7月22日,中国证券报记者从产业链人士处独家获悉,进入7月,苹果iPhone 18系列手机已在量产,...
29岁的党鑫蕊倒在广西泥泞山路... 7月11日晚一通叮嘱,成了父子最后的对话;几天后父亲在殡仪馆见到一身泥的儿子,才知道“他走得有多难。...