Development in MongoDB Java environment

Source: Internet
Author: User
Tags mongoclient

As with MySQL, Oracle, the first thing to download is the driver package.

Open MyEclipse and create a Java project.

Create a Lib folder under the project, place the downloaded driver into Lib and build to path:

Then build a db.properties file under SRC

Host=127.0.0.1port=27017dbname=test

Create a new util package and create a new class under package Dbutil

package util;import java.io.ioexception;import java.net.unknownhostexception;import  java.util.properties;import com.mongodb.dbcollection;import com.mongodb.dbobject;import  com.mongodb.mongoclient;import com.mongodb.util.json;public class dbutil {private  Static properties p=new properties ();p rivate static string host;private  Static int port;private static string dbname;static{try {p.load ( DBUtil.class.getClassLoader (). getResourceAsStream ("Db.properties"); host= (String)  p.get ("host");p ort= Integer.valueof (String)  p.get ("Port"));d bname= (String)  p.get ("dbname");}  catch  (ioexception e)  {e.printstacktrace ();}} Public static dbcollection getdbcollection (String tablename) {try { return  New mongoclient (Host,port). Getdb (dbname). GetCollection (tablename);}  catch  (unknownhostexception e) &NBSp {E.printstacktrace ();} Return null;} Public static dbobject getdbcursorfromjson (String jsondata) {return  (DBObject)   Json.parse (jsondata);} Public static string getstringdatafromdbobject (dbobject entry) {return JSON.serialize ( Entry);}}

Here's a simple crud test

Package testmongodb;import org.bson.types.objectid;import util. dbutil;import com.mongodb.basicdbobject;import com.mongodb.dbcollection;import  com.mongodb.dbcursor;import com.mongodb.dbobject;public class test {@org. Junit.Testpublic  void testadd () {dbcollection collection=dbutil.getdbcollection ("testtable");D bobject user= New basicdbobject (); User.put ("Name",  "Zhangsan"); User.put ("Age",  11); Collection.insert (user). Getn ();} @org. Junit.testpublic void testfind () {//MONGODB does not support federated queries, subqueries, which need to be done by ourselves in the program. The result set of the query is filtered in the Java query as needed. Dbcollection collection=dbutil.getdbcollection ("TestTable");D bobject user=new basicdbobject (); User.put ("name",  "Zhangsan") user.put ("Age",  "11");//All Queries//dbcursor cursor=collection.find ();// Conditional Query Dbcursor cursor=collection.find (new basicdbobject ("Age",  new basicdbobject ("$lte",  105));while  (Cursor.hasnext ())  {system.out.println (Cursor.next ());}} @org. Junit.testpublic void testdelete () {dbcollection collection=dbutil.getdbcollection (" TestTable ");//delete Collection.remove (New basicdbobject (" _id ",  new objectid (" 5590e57b7d51ad2ef1b69a93 ")));//Condition Delete collection.remove (New basicdbobject (" Age ",  new basicdbobject ("$gte",  105));} @org. Junit.testpublic void testupdate () {dbcollection collection=dbutil.getdbcollection (" TestTable ");//Modify Collection.update (New basicdbobject (" _id ",  new objectid (" 5590e57b7d51ad2ef1b69a93 ")),  new basicdbobject (" Age ",  99));//Condition Modification collection.update (new  Basicdbobject ("Age",  new basicdbobject ("$gte",  105)),  new basicdbobject ("Age",  11));}}

About aggregations can see this article, article address

About index can see this article, article address

Finally, MongoDB uses the opposite side of the way to process the database, so that developers do not have to care about SQL issues, but also limit the business he can handle, for complex data analysis or complex relational ERP, etc., is not suitable. But there is a tradeoff between using a traditional database for a table for complex analysis, and mongodb for simplicity.

Development in MongoDB Java environment

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.