您好,登录后才能下订单哦!
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<Document> 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<Document> 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<String> iterable = database.listCollectionNames();
try (final MongoCursor<String> it = iterable.iterator()) {
while (it.hasNext()) {
if (it.next().equalsIgnoreCase(collectionName)) {
return true;
}
}
}
return false;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。