By opening the MongoDB jar package you can see that the MONGO class has many methods, including getaddress (), Dropdatabase (String), Getdb (string), and so on. Here are one by one demonstrations of the use of Mongo.class.
The demo of MONGO is done primarily in a test class, so I've built a junittest_1 Java class here. The main code is as follows, because the comments are rich, not explained.
Import Com.mongodb.Mongo;
public class Junittest_1 {
@Test
public void Test1 () throws exception{
Mongo Mongo = new Mongo ();
Get all the databases
List<string> dbs = Mongo.getdatabasenames ();
for (String Db:dbs) {
SYSTEM.OUT.PRINTLN (DB);
}
Get the current MongoDB version
System.out.println ("MONGO Version =" + Mongo.getversion ());
Get server address
SYSTEM.OUT.PRINTLN ("MONGO server address =" + mongo.getaddress ());
Delete a database
Mongo.dropdatabase ("test");
You can also view the maximum setting value for a Bson document
System.out.println ("bsonobjectsize =" + mongo.getmaxbsonobjectsize ());
Debugstring
System.out.println ("debugstring =" + mongo.debugstring ());
Mongo.close ();
}
}
Running on my computer, the results of the output are as follows:
Local
Test
MONGO Version = 2.10.1
MONGO server address =/127.0.0.1:27017
Bsonobjectsize = 16777216
debugstring = Dbtcpconnector:/127.0.0.1:27017/127.0.0.1:27017
In practice, it is recommended to use mongoclient instead of the MONGO class. The calling test code for the Mongoclient class is as follows
public void Test2 () throws exception{
Mongoclient client = new Mongoclient ();
Querying all the current databases
List<string> dbs = Client.getdatabasenames ();
for (String Db:dbs) {
SYSTEM.OUT.PRINTLN (DB);
}
Get the current MongoDB version
System.out.println ("MONGO Version =" + Client.getversion ());
Get server address
SYSTEM.OUT.PRINTLN ("MONGO server address =" + client.getaddress ());
Delete a database
Client.dropdatabase ("test");
You can also view the maximum setting value for a Bson document
System.out.println ("bsonobjectsize =" + client.getmaxbsonobjectsize ());
Debugstring
System.out.println ("debugstring =" + client.debugstring ());
Client.close ();
}
The print results are as shown.
Java Manipulation Mongodb_3 (a preliminary study of MongoDB)