Tuna Töre

> Coder

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}

02/02/2015 Posted by | MongoDB | | Leave a comment

ubuntu activate root user


command sudo passwd root
will activate root user

Capture

31/01/2015 Posted by | Ubuntu | Leave a comment

How many years of programming experience do you have?


21/01/2015 Posted by | Poll | Leave a comment

org.xml.sax.SAXParseException: Current configuration of the parser doesn’t allow a maxOccurs attribute value to be set greater than the value 5,000


org.xml.sax.SAXParseException: Current configuration of the parser doesn't allow a maxOccurs attribute value to be set greater than the value 5,000.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.reportSchemaFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.checkAttributes(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.checkAttributes(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source)

The solution for this problem; you should disable FEATURE_SECURE_PROCESSING

//Create SchemaFactory using XSD file
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,false);
Schema schema = factory.newSchema(xsdSource);

20/10/2014 Posted by | XML, XSD | , | Leave a comment

JSF: getOutputStream() has already been called for this response


If you get the following exception on JSF web applications;

getOutputStream() has already been called for this response

you should tell JSF context that the outputstream is completed by programmatically

solution

FacesContext.getCurrentInstance().responseComplete();

01/10/2014 Posted by | JSF | Leave a comment

some photos


DSC_0765_ DSC_0848 DSC_0694 tt DSC_0038 DSC_0050

DSC_0030

20/08/2014 Posted by | Photography | Leave a comment

Budapest Photos


DSC_0121-001


DSC_0192-001


DSC_0023-001


DSC_0070-001


DSC_0045-001


13/08/2014 Posted by | Photography | Leave a comment

Good example/document for Correlated subquery


You can check the following page for Correlated Subqueries

Especially update query

https://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fsqlp%2Frbafyexsub4.htm

basic syntax

UPDATE TABLE_NAME X
SET FIELD=(SELECT Y.ANYFIELD
FROM ANOTHERTABLE Y
WHERE X.ID= Y.FID)
WHERE X.ID IN (SELECT FID
FROM ANOTHERTABLE)

UPDATE EMPLOYEE X
SET NEWSALARY=( SELECT Y.NEWSALARY
FROM SALARIES Y
WHERE X.EMPID = Y.EMPID)
WHERE X.EMPID IN ( SELECT EMPID
FROM SALARIES)

29/01/2014 Posted by | Oracle, Oracle SQL | Leave a comment

Android Development Tools for Eclipse book review (packt publishing)


Today, I finished reading ADT for Eclipse book;

In my opinion, if you are new for the Android Mobile Dev. Platform, you should consider reading this book.

In total, this book includes 144 pages so it is very easy to read. Authors did very good job by summarizing important subjects into one small sized book.

You will find essential details regarding ADT and this will enables you to code and publish your mobile apps.

Pros Regarding the book;

+Concise and clear explanations
+Including screenshots
+Relevant content

Cons

+Could include more chapters regarding Android Virtual Device optimization
+Some screenshots are based on Windows others for Linux

You can review the chapters of the book by clicking the following link;

 Reading Android Development Tools for Eclipse Book  Reading Android Development Tools for Eclipse Book

17/10/2013 Posted by | Android | Leave a comment

Android FREE PhotoSync App (Facebook&Dropbox)


Hi bloggers,

Here you can find a FREE Android App developed by me;

PhotoSync

Click on this link to download

https://play.google.com/store/apps/details?id=air.com.ttore.AutoPhotoPublisher&hl=en

Product Description;
Using this app you won’t worry publishing your photos to Facebook and/or DropBox together.
This mobile app will publish your photos based on your interaction(manual publishing mode) or automatically (auto publishing mode) to Facebook and/or DropBox.Auto publishing Mode will give you the power of syncing your photos on two different platforms (Facebook and DropBox) at the same time. In order to do that you just only need to connect your accounts to this app and publishing will be handled automatically. This way for example while you are travelling and taking some nice cool photos, social publishing will be done on Facebook and one copy is also saved in DropBox.

Functions;
Facebook/DropBox connection
Auto Photo Publishing to Facebook&DropBox
Manual Photo Publishing to Facebook&DropBox

Connecting DropBox;
Please select remember me in order to prevent connection problems.

09/10/2013 Posted by | Android | , | Leave a comment

Follow

Get every new post delivered to your Inbox.