MongoDB Java API Example code


Hi here you can find a java code for basic java operations on MongoDB for a reference


package mongodbtest;

import java.net.UnknownHostException;
import java.util.Arrays;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;

public class MongoTest {

	public static void main(String[] args) {
		
		try {
			
			MongoClient client = new MongoClient("localhost", 27017);
			DB db = client.getDB("testdb");
								
			//getting users table from mongodb/testdb		
			DBCollection table = db.getCollection("users");
			table.drop();
			//this will create users table/collection
			table = db.getCollection("users");			
			
			// inserting key and value
			BasicDBObject document = new BasicDBObject();
			document.put("username", "tuna");
			document.put("password", "2369081290");
			table.insert(document);

			BasicDBObject document2 = new BasicDBObject();
			document2.put("username", "tom");
			document2.put("password", "12345678");
			table.insert(document2);
			
			//display documents
			System.out.println("users table elements count : " + table.count());
			
			//listing all documents in users table
			DBCursor cursor = table.find();
			 
			while (cursor.hasNext()) {
				System.out.println("doc - " + cursor.next());
			}
						
			//whereQuery 
			BasicDBObject whereQuery = new BasicDBObject();
			whereQuery.put("username", "tuna");			
			DBCursor cursor2 = table.find(whereQuery);
			 					
			while (cursor2.hasNext()) {
				System.out.println("\nget document having username tuna - " + cursor2.next() +"\n");
			}
						
			//inserting bulk documents
			for (int i = 0; i < 10; i++) {
				document = new BasicDBObject();
				document.put("username", "test-" + i);
				document.put("password", "pass-" + i);
				table.insert(document);
			}
			 
			//getting all documents
			cursor = table.find();
			while (cursor.hasNext()) {
				System.out.println("doc - " + cursor.next());
			}
			
			//removing a document
			BasicDBObject query = new BasicDBObject();
			query.put("username", "tom");
			table.remove(query);
			
			//getting all documents
			cursor = table.find();
			System.out.println();
			while (cursor.hasNext()) {
				System.out.println("doc - " + cursor.next());
			}
			
			//inserting bulk documents
			for (int i = 0; i < 10; i++) {
				document = new BasicDBObject();
				document.put("username", "test2-" + i);
				document.put("password",  i);
				table.insert(document);
			}
			 
			//getting all documents
			cursor = table.find();
			System.out.println();
			while (cursor.hasNext()) {
				System.out.println("doc - " + cursor.next());
			}
			
			//logical query
			BasicDBObject and = new BasicDBObject();			
			and.put("$and", Arrays.asList(new BasicDBObject("username", "test2-3"),new BasicDBObject("password", 3)));
			
			DBCursor cursor3 = table.find(and);
			while (cursor3.hasNext()) {
				System.out.println("\ndoc - " +cursor3.next());
			}
			
			//numbers filtering
			BasicDBObject filter = new BasicDBObject();		
			filter.put("password", new BasicDBObject("$gt", 4));			
			
			System.out.println();
			//filtering pass greater than 4
			cursor3 = table.find(filter);
			while (cursor3.hasNext()) {
				System.out.println("doc - " +cursor3.next());
			}
						
			//updating a document
			BasicDBObject document3 = new BasicDBObject();
			document3.append("$set", new BasicDBObject("username", "new user name"));		 
			BasicDBObject searchQuery = new BasicDBObject("username", "test2-9");		 
			table.update(searchQuery, document3);
			
			//getting all documents
			cursor = table.find();
			System.out.println();
			while (cursor.hasNext()) {
				System.out.println("doc - " + cursor.next());
			}
			
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}


Output of the program

users table elements count : 2
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9c”} , “username” : “tuna” , “password” : “2369081290”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9d”} , “username” : “tom” , “password” : “12345678”}

get document having username tuna – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9c”} , “username” : “tuna” , “password” : “2369081290”}

doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9c”} , “username” : “tuna” , “password” : “2369081290”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9d”} , “username” : “tom” , “password” : “12345678”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9e”} , “username” : “test-0” , “password” : “pass-0”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9f”} , “username” : “test-1” , “password” : “pass-1”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca0”} , “username” : “test-2” , “password” : “pass-2”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca1”} , “username” : “test-3” , “password” : “pass-3”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca2”} , “username” : “test-4” , “password” : “pass-4”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca3”} , “username” : “test-5” , “password” : “pass-5”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca4”} , “username” : “test-6” , “password” : “pass-6”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca5”} , “username” : “test-7” , “password” : “pass-7”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca6”} , “username” : “test-8” , “password” : “pass-8”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca7”} , “username” : “test-9” , “password” : “pass-9”}

doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9c”} , “username” : “tuna” , “password” : “2369081290”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9e”} , “username” : “test-0” , “password” : “pass-0”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9f”} , “username” : “test-1” , “password” : “pass-1”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca0”} , “username” : “test-2” , “password” : “pass-2”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca1”} , “username” : “test-3” , “password” : “pass-3”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca2”} , “username” : “test-4” , “password” : “pass-4”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca3”} , “username” : “test-5” , “password” : “pass-5”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca4”} , “username” : “test-6” , “password” : “pass-6”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca5”} , “username” : “test-7” , “password” : “pass-7”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca6”} , “username” : “test-8” , “password” : “pass-8”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca7”} , “username” : “test-9” , “password” : “pass-9”}

doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9c”} , “username” : “tuna” , “password” : “2369081290”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9e”} , “username” : “test-0” , “password” : “pass-0”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9f”} , “username” : “test-1” , “password” : “pass-1”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca0”} , “username” : “test-2” , “password” : “pass-2”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca1”} , “username” : “test-3” , “password” : “pass-3”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca2”} , “username” : “test-4” , “password” : “pass-4”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca3”} , “username” : “test-5” , “password” : “pass-5”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca4”} , “username” : “test-6” , “password” : “pass-6”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca5”} , “username” : “test-7” , “password” : “pass-7”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca6”} , “username” : “test-8” , “password” : “pass-8”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca7”} , “username” : “test-9” , “password” : “pass-9”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca8”} , “username” : “test2-0” , “password” : 0}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca9”} , “username” : “test2-1” , “password” : 1}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46caa”} , “username” : “test2-2” , “password” : 2}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cab”} , “username” : “test2-3” , “password” : 3}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cac”} , “username” : “test2-4” , “password” : 4}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cad”} , “username” : “test2-5” , “password” : 5}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cae”} , “username” : “test2-6” , “password” : 6}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46caf”} , “username” : “test2-7” , “password” : 7}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cb0”} , “username” : “test2-8” , “password” : 8}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cb1”} , “username” : “test2-9” , “password” : 9}

doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cab”} , “username” : “test2-3” , “password” : 3}

doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cad”} , “username” : “test2-5” , “password” : 5}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cae”} , “username” : “test2-6” , “password” : 6}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46caf”} , “username” : “test2-7” , “password” : 7}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cb0”} , “username” : “test2-8” , “password” : 8}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cb1”} , “username” : “test2-9” , “password” : 9}

doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9c”} , “username” : “tuna” , “password” : “2369081290”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9e”} , “username” : “test-0” , “password” : “pass-0”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46c9f”} , “username” : “test-1” , “password” : “pass-1”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca0”} , “username” : “test-2” , “password” : “pass-2”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca1”} , “username” : “test-3” , “password” : “pass-3”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca2”} , “username” : “test-4” , “password” : “pass-4”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca3”} , “username” : “test-5” , “password” : “pass-5”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca4”} , “username” : “test-6” , “password” : “pass-6”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca5”} , “username” : “test-7” , “password” : “pass-7”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca6”} , “username” : “test-8” , “password” : “pass-8”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca7”} , “username” : “test-9” , “password” : “pass-9”}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca8”} , “username” : “test2-0” , “password” : 0}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46ca9”} , “username” : “test2-1” , “password” : 1}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46caa”} , “username” : “test2-2” , “password” : 2}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cab”} , “username” : “test2-3” , “password” : 3}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cac”} , “username” : “test2-4” , “password” : 4}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cad”} , “username” : “test2-5” , “password” : 5}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cae”} , “username” : “test2-6” , “password” : 6}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46caf”} , “username” : “test2-7” , “password” : 7}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cb0”} , “username” : “test2-8” , “password” : 8}
doc – { “_id” : { “$oid” : “54cf56b02435f15a5ca46cb1”} , “username” : “new user name” , “password” : 9}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s