MongoDB with Java

In previous section, we have discussed about the MongoDB tools and use of them in MongoDB database. In this section, we will discuss some example on MongoDB with Java and will see how to implement all the commands in this example.

We know that we can use Mongo DB database in java application in which we can apply insert, select, update, delete on to the database. Following example of java is based on the sample operation on Mongo DB database. For the successive completion of java program with Mongo DB we have to follow the following such steps.

Step-1:

First of all write the java source code using mongo DB. Save it to the root folder. In my example the root folder is C:\\mongodb. Sample source code for inserting some documents to the Mongo DB database.

import com.mongodb.*;

public class MongoDemo{
	public static void main (String args[])
	{
		try{
			Mongo mongo=new Mongo();
			DB db=mongo.getDB("test"); 
			DBCollection product=db.getCollection("product");
			BasicDBObject doc=null;
			
			//creating 1st element 
			doc=new BasicDBObject();
			doc.put("name", "pratik");
		    doc.put("age", "22");
			product.insert(doc);
			
			//creating 2nd element 
			doc=new BasicDBObject();
			doc.put("name", "mamuni");
			doc.put("age", "22");
		    product.insert(doc);
			
			//creating 3rd element 
			doc=new BasicDBObject();
			doc.put("name", "archu");
			doc.put("age", "22");
			product.insert(doc);
			
			DBCursor cursor=product.find();
			while(cursor.hasNext())
			{
				System.out.println(cursor.hasNext());
			}
		}
		catch(java.net.UnknownHostException e)
		{
			e.printStackTrace();		
		}
	}
}

Description of the above java code:

The line 1 that is import com.mongodb.* is used for importing all the Class file of Mongo DB into our java code by which we can use the all Class file in our java program.

From line 2 to line 6 is known as basic rule of class definition in java. In line 8 we are creating the reference of Mongo class, by using this reference we can call the various operation in mongo shell.

After making the reference of Mongo class, in line 9, we are calling the getDB() which has one parameter that is the database name into which we are going to insert the document. By default the database name is “test” so in the above example we are inserting the document in the “test” database. But we can pass any database name to the getDB() method through which we want to insert the document. And store the return value of getDB to the reference of DB class that is db in this case.

After getting the database name, in line 10 , get the collection name to which we are inserting the document because in Mongo DB we are storing the document in the Collections. If we want to store the document in a new Collection so we can pass the Collection name as parameter od getCollection() method. And this collection name is stored in the reference of the DBCollection  Class.

After getting the collection name, in line 11, we are making a reference of BasicDBObject that is doc through which we can perform various number of command like insert, update, remove etc. and then initialize reference doc which is shown in line number 11.

doc.put() method is generally used for putting the key and value doc. From line number 14 to line number 16, the code is showing that the multiple key value pair is storing in doc. In line number 17 we are inserting the doc into the Collection. Here the collection name is “product”. Same operation is performing from line number 20 to line number 29 for inserting the 2nd and 3rd document. 

From line number 31 to line number 35, there is a reference of DBCursor which can move begging to end in the documents. Product.find() signifies that to show al the documents which are reside in that collection. Cursor.hasnext define the next document in the collection if the documents are existing.

These are the description of the above java source code which is storing the data or documents inside the Mongo DB database.

Procedure for execution this example:

Step-1:

Open the command prompt and connect to Mongo DB server bye the executing the command mongod that is mongo DB server to start.

MongoDB with Java

Step-2:

After the connection to Mongo DB server is over then open folder where the java file is stored. Before compiling the code first import the jar file which name is mongo-2.10.1.jar by the following command.1. this jar file contains the all class file of Mongo DB.

Set classpath =folder name:\\ mongo-2.10.1.jar

Step-3:

After set classpath compile the java code.

Javac mongodemo.java

Output:

In this section, we have discussed a example for MongoDB with Java. In next section we will understand another example for MongoDB with Java.

Leave a Reply

Your email address will not be published. Required fields are marked *