Java and MongoDB link for project

Source: Internet
Author: User
Tags mongoclient

Given the development of open source projects, embrace the open source community. Discovering Java and MongoDB is a good choice.

As with other databases, Mongo-java-driver is also required to build the interaction between Java and MongoDB.

1. Connect MongoDB

1.1 Connection to a normal database

Mongoclient mongoclient = new Mongoclient ();
Mongoclient mongoclient = new Mongoclient ("localhost");
Mongoclient mongoclient = new Mongoclient ("localhost", 27017);
Mongoclient mongoclient = new Mongoclient (arrays.aslist (New serveraddress ("localhost", 27017), New ServerAddress (" localhost ", 27018), New serveraddress (" localhost ", 27019));

1.2 Database connections with permission requirements

Mongocredential credential = mongocredential.createmongocrcredential (userName, database, password); Mongoclient mongoclient = new Mongoclient (new ServerAddress (), Arrays.aslist (credential));

2. Connect to MongoDB specific database

2.1 Connecting to a specific database

DB db = Mongoclient.getdb ("MyDB");

2.2 Print all databases

List<string> dbs = Mongoclient.getdatabasenames (); for (String Db:dbs) {    System.out.println (db);}

3. Get a specific collection in the database

3.1 Connecting a specific collection

Dbcollection coll = db.getcollection ("testcollection");

3.2 Print all collections

set<string> tables = Db.getcollectionnames (); for (String coll:tables) {    System.out.println (coll);}

4. Inserting Data items

Basicdbobject doc = new Basicdbobject ("name", "MongoDB")        . Append ("type", "Database")        . Append ("Count", 1)        . Append ("Info", New Basicdbobject ("X", 203). Append ("Y", 102)); Coll.insert (DOC);

5. Querying Data items

5.1 Querying the latest data item

DBObject MyDoc = Coll.findone (); System.out.println (MYDOC);

5.2 Querying all data items

dbcursor cursor = Coll.find (); try {while   (Cursor.hasnext ()) {       System.out.println (Cursor.next ())}   } finally {   cursor.close ();}

5.3 Number of data items queried

System.out.println (Coll.getcount ());

5.4 Querying specific data items

5.4.1 equals a condition

Basicdbobject query = new Basicdbobject ("I", +); cursor = coll.find (query); try {while   (Cursor.hasnext ()) {       System.out.println (Cursor.next ());}   } finally {   cursor.close ();}

5.4.2 greater than or less than a certain condition

Find all where i > 50query = new Basicdbobject ("I", New Basicdbobject ("$GT"), cursor = coll.find (query), try {
   while (Cursor.hasnext ()) {        System.out.println (Cursor.next ());}    } finally {    cursor.close ();}

6. Updating data items

Dbcollection table = db.getcollection ("user"); Basicdbobject query = new Basicdbobject (); Query.put ("Name", "Mkyong"); Basicdbobject newdocument = new Basicdbobject () newdocument.put ("name", "mkyong-updated"); Basicdbobject updateobj = new Basicdbobject () updateobj.put ("$set", newdocument); Table.update (query, updateobj);

7. Deleting data items

Dbcollection table = db.getcollection ("user"); Basicdbobject searchQuery = new Basicdbobject () searchquery.put ("name", "Mkyong"); Table.remove (SearchQuery);

Java and MongoDB link for project

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.