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;

}


相关内容

热门资讯

今日重大发现“乐乐四川麻将.是... 家人们!今天小编来为大家解答乐乐四川麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
史上最大IPO,要来了! 【导读】SpaceX估值升至8000亿美元,确认计划于2026年IPO 中国基金报记者 泰勒 大家好...
终于了解“乐驿茶坊.怎么开挂?... 有 亲,根据资深记者爆料乐驿茶坊是可以开挂的,确实有挂(咨询软件无需打开...
玩家攻略科普“新世界牛牛.真的... 有 亲,根据资深记者爆料新世界牛牛是可以开挂的,确实有挂(咨询软件无需打...
终于明白“全民内蒙古麻将.有没... 您好:全民内蒙古麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这...
玩家攻略科普“来来淮北麻将.怎... 您好:来来淮北麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款...
【今日要闻】“飞鹰互娱牛牛.有... 网上科普关于“飞鹰互娱牛牛有没有挂”话题很是火热,小编也是针对飞鹰互娱牛牛作*弊开挂的方法以及开挂对...
终于明白“战皇炸金花.真的有挂... 终于明白“战皇炸金花.真的有挂吗?”其实是有挂您好,战皇炸金花这个游戏其实有挂的,确实是有挂的,需要...
今日重大通报“小乐红中麻将.究... 家人们!今天小编来为大家解答小乐红中麻将透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里...
【今日要闻】“同城跑胡子.到底... 您好:同城跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游...