Use of Mongodb_java connection MongoDB database mongo/mongoclient __mongodb

Source: Internet
Author: User
Tags mongoclient mongodb driver

Originally want to write a Java operation MongoDB database additions and deletions to check the demo, but recently there is no time, only the previous record of the connection MongoDB method posted ...

And so have time to complete the additions and deletions to check the demo to fill it ....

I installed the local MongoDB database, so I was running under windos ...

No jar package ... Look here....

Java connection MongoDB Driver jar Pack


These constants ... Modify it according to your actual environment ... If the MONGO database does not have a username and password, do not check ...

	Private static final String host = "localhost";
	private static final int port = 27017;
	private static final String userName = "";
	Private static final String password = "";
	private static final String DataBaseName = "Mongotest";
	Private static final String tablename = "User";

Version one, pay attention to see: I use here is Mongo Mongo = new Mongo (host, Port); To connect with ...

	
	public void Connmongodb () throws Exception {
		Mongo Mongo = new Mongo (host, port);
		DB db = Mongo.getdb (dataBaseName);
		if (! Stringutils.isempty (userName) | | ! Stringutils.isempty (password)) {
			db.authenticate (UserName, Password.tochararray ());
		}
		Dbcollection dbcollection = db.getcollection (tablename);
		Dbcursor dbcursor = Dbcollection.find ();
		while (Dbcursor.hasnext ()) {
			map map = (map) dbcursor.next ();
			SYSTEM.OUT.PRINTLN (map);
		}
	

Result diagram:

This is my MongoDB database.



Then the version 2.

Pay attention to see Ah ... Here I am using mongoclient mongoclient = new Mongoclient (host,port); Connect the MongoDB ...

	
	public void Connectmongodb () {
		try{
			mongoclient mongoclient = new Mongoclient (host,port);
			DB db = Mongoclient.getdb (dataBaseName);
			Dbcollection dbcollection = db.getcollection (tablename);
			Dbcursor dbcursor = Dbcollection.find ();
			while (Dbcursor.hasnext ()) {
				System.out.println (Dbcursor.next ());
			}
		} catch (Exception e) {
			e.printstacktrace ();
		}
	}
	

Although their results are the same.



They can all connect to the MongoDB database, but what difference does it make?

Baidu Check the next, find this article, analysis of the good, interested can see ...

However, official documentation and source code suggest using the Mongoclient class ... No, the future will be abandoned MONGO

Analysis on difference of writing performance between Mongoclient and MONGO


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.