Java connects to the MongoDB database and javamongodb Database
I tried MongoDB during this time, and it is very easy to use and convenient. It has a great advantage over relational databases. So I tried to connect to MongoDB using java and
Basic addition, deletion, modification, and query operations.
First, connect to the database on the console and check the tables in the database.
Now, create a maven project. The dependencies in pom. xml are as follows:
<!-- WICKET DEPENDENCIES --><dependency><groupId>org.apache.wicket</groupId><artifactId>wicket</artifactId><version>${wicket.version}</version></dependency><!-- OPTIONAL <dependency><groupId>org.apache.wicket</groupId><artifactId>wicket-extensions</artifactId><version>${wicket.version}</version></dependency>--><!-- LOGGING DEPENDENCIES - LOG4J --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.4.2</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.14</version></dependency><!-- JUNIT DEPENDENCY FOR TESTING --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--mongoDB for test--><dependency><groupId>org.mongodb</groupId><artifactId>mongo-java-driver</artifactId><version>3.0.2</version></dependency><!-- JETTY DEPENDENCIES FOR TESTING --><dependency><groupId>org.mortbay.jetty</groupId><artifactId>jetty</artifactId><version>${jetty.version}</version><scope>provided</scope></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>jetty-util</artifactId><version>${jetty.version}</version><scope>provided</scope></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>jetty-management</artifactId><version>${jetty.version}</version><scope>provided</scope></dependency></dependencies>
Now, create a MongoDb class in the source folder
Write the following nine methods:
Package com. haizhi. mongoDB. connect; import com. mongodb. *; import java. util. list; import java. util. map; import java. util. set;/*** Created by xiaxuan on 15/7/17. */public class MongoDb {// 1. Connect to the public MongoClient getMongo () {// 1. Connect to the MongoDB database MongoClient mongo = new MongoClient ("127.0.0.1", 27017 ); return mongo;} // 2. Obtain the public DB getDb (String DbName) {response client mongo = getMongo (); return mongo. getDB (DbName);} // 3. display all databases in the database public void showDb () {MongoClient mongo = getMongo (); // display the List of all databases <String> dbs = mongo. getDatabaseNames (); for (String database: dbs) {System. out. println (database) ;}}// 4. Obtain a table public DBCollection getTable (DB db, String tableName) {// If the table does not exist, mongoDB will create a DBCollection table = db. getCollection ("users"); return table;} // 5. display all tables in the database public void showTables (DB db) {// display all the tables in the selected database. Set <String> tables = db. getCollectionNames (); for (String coll: tables) {System. out. println (coll) ;}}// 6. Add a public void insert (Map <String, Object> map, DBCollection table) {BasicDBObject document = new BasicDBObject (); for (Object key: map. keySet () {document. put (String) key, map. get (key);} table. insert (document);} // 7. search for and display a log public void search (BasicDBObject basicDBObject, DBCollection table) {DBCursor dbCursor = table. find (basicDBObject); while (dbCursor. hasNext () {System. out. println (dbCursor. next ();} System. out. println ("End");} // 8. update a public void update (BasicDBObject query, BasicDBObject newDocument, DBCollection table) {BasicDBObject updateObj = new BasicDBObject (); updateObj. put ("$ set", newDocument); table. update (query, updateObj);} // 9. delete a public void delete (BasicDBObject basicDBObject, DBCollection table) {table. remove (basicDBObject );}}
Now, in the test folder, create a test class:
testInsert:
package com.haizhi.testMongoDB;import com.haizhi.mongoDB.connect.MongoDb;import com.mongodb.DBCollection;import com.mongodb.DB;import com.mongodb.MongoClient;import org.junit.Test;import java.util.HashMap;import java.util.Map;/** * Created by xiaxuan on 15/7/17. */public class testInsert { @Test public void insert(){ MongoDb mongoDb=new MongoDb(); MongoClient mongo=mongoDb.getMongo(); DB dB=mongoDb.getDb("MongoTest"); DBCollection table=mongoDb.getTable(dB, "users"); Map<String,Object> map=new HashMap<String, Object>(); map.put("id",3); map.put("name","xiaxuan"); map.put("age",22); map.put("sex",true); map.put("nickname","bingwen"); mongoDb.insert(map,table); }}
After the program is executed, search in the database:
This record is successfully inserted in the database.
Test query now:
TestSearch:
package com.haizhi.testMongoDB;import com.haizhi.mongoDB.connect.MongoDb;import com.mongodb.BasicDBObject;import com.mongodb.DBCollection;import com.mongodb.MongoClient;import com.mongodb.DB;import org.junit.Test;/** * Created by xiaxuan on 15/7/17. */public class testSearch { @Test public void testSearch(){ MongoDb mongoDb=new MongoDb(); MongoClient mongo=mongoDb.getMongo(); DB dB=mongoDb.getDb("MongoTest"); DBCollection talbe=mongoDb.getTable(dB, "users"); BasicDBObject basicDBObject=new BasicDBObject(); basicDBObject.append("name","xiaxuan"); mongoDb.search(basicDBObject,talbe); }}
Two pieces of data are successfully queried.
The modified test source code is basically the same as the source code to be deleted. Here, only the source code is pasted and the specific effect is not demonstrated.
TestUpdate:
package com.haizhi.testMongoDB;import com.haizhi.mongoDB.connect.MongoDb;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.sun.xml.internal.rngom.parse.host.Base;import org.junit.Test;/** * Created by xiaxuan on 15/7/17. */public class testUpdate { @Test public void update(){ MongoDb mongoDb=new MongoDb(); DB dB=mongoDb.getDb("MongoTest"); DBCollection table=mongoDb.getTable(dB, "users"); BasicDBObject query=new BasicDBObject(); query.put("name","xiaxuan"); BasicDBObject newDocument=new BasicDBObject(); newDocument.put("name","bingwen"); mongoDb.update(query,newDocument,table); }}
TestDelete:
package com.haizhi.testMongoDB;import com.haizhi.mongoDB.connect.MongoDb;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import org.junit.Test;/** * Created by xiaxuan on 15/7/17. */public class testDelete { @Test public void delete(){ MongoDb mongoDb=new MongoDb(); DB dB=mongoDb.getDb("MongoTest"); DBCollection table=mongoDb.getTable(dB, "users"); BasicDBObject basicDBObject=new BasicDBObject(); basicDBObject.put("name","xiaxuan"); mongoDb.delete(basicDBObject,table); }}
Connecting to Mongodb and basic addition, deletion, modification, and query operations are simple.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.