Java Manipulation Mongodb_3 (a preliminary study of MongoDB)

Source: Internet
Author: User
Tags mongoclient mongodb version

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.