public static void Main (string[] args) {
Mongoclientoptions.builder build = new Mongoclientoptions.builder ();
Maximum number of connections to data 50
Build.connectionsperhost (50);
If all current connection are in use, there can be 50 threads queued for each connection
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);
Gets the database test, does not exist, it automatically establishes the database
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 must be close to release resources after use
Client.close ();
System.out.println ("Ooooooooooooookkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
}
Determine if collection exists
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;
}
MONGODBDB enable Wiredtiger engine and zlib compression