Friday, October 3, 2014

Index and query with mongo author JK

Indexing and querying with MongoDB using suitable example.
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;
import java.util.List;
public class IndexQuery{
public static void main( String args[] ){
try{
// To connect to mongodb server
MongoClient mongoClient = new MongoClient( "192.168.5.122" , 27017 );
// Now connect to your databases
DB db = mongoClient.getDB( "te3355db" );
System.out.println("Connect to database successfully");
String user="te3355";
String password="te3355"; // returns 'l'
char[] c_arr = password.toCharArray();

boolean auth = db.authenticate(user,c_arr);
System.out.println("Authentication: "+auth);
DBCollection col=db.getCollection("Feedback");
System.out.println("Collection is selected successfully!!!");
System.out.print("----------------------------------------");
DBCursor cursor=col.find();
int i=1;
System.out.println("\n INSERTED DOCUMENT IN Feedback COLLECTION");
      while(cursor.hasNext())
      {
            System.out.println(cursor.next());
            i++;
      }
BasicDBObject ob=new BasicDBObject();
ob.put("Roll", 1);
ob.put("Name", -1);
col.ensureIndex(ob);
List<DBObject>indices=col.getIndexInfo();
int th=0;
System.out.println("\n INDEX INFORMATION:");
for(DBObject dbo: indices)
{
      System.out.println("["+ th++ +"]"+dbo);
     
}

//create the collection
//db.createCollection("Example_sample3", ob);
System.out.println("----------------------------------------");
System.out.println("Collection is created suucessfully!!!");
System.out.println("----------------------------------------");
//inserting in the Exmple_sample
DBCollection  collection=db.getCollection("Example_sample3");
BasicDBObject doc=new BasicDBObject("TITLE","MOVIE");
      doc.append("Name", "Avtar");
      doc.append("Category", "Motion Making");
      doc.append("Rating", "8.5");
     
      collection.insert(doc);
DBCursor cur=collection.find();
int v=1;
while(cur.hasNext())
{
      System.out.println(cur.next());
      v++;
}

//updating the document
BasicDBObject ex=new BasicDBObject();
ex.put("Category", "Animation and motion making");
BasicDBObject searchQuery = new BasicDBObject().append("TITLE", "MOVIE");
collection.update(searchQuery,ex);
System.out.println("Document updated successfully");
System.out.println("Updated document.");
System.out.println("----------------------------------------");
DBCursor curs=collection.find();
int s=1;
while(curs.hasNext())
{
      System.out.println(curs.next());
      s++;
}
//REMOVING THE FIRST DOCUMENT.

DBObject myob=col.findOne();
col.remove(myob);
DBCursor cursorl=col.find();
int k=1;
System.out.println("\n INSERTED DOCUMENT IN Feedback COLLECTION AFTER REMOVING FIRST DOCUMENT");
      while(cursorl.hasNext())
      {
           
            System.out.println(cursorl.next());
            k++;
     
      }

}
catch(Exception e)
      {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      }
     
}
}


OUTPUT:
Connect to database successfully
Authentication: true
Collection is selected successfully!!!
----------------------------------------
 INSERTED DOCUMENT IN Feedback COLLECTION
{ "_id" : { "$oid" : "541bff0c532a2e5b3a4ff99d"} , "Name" : "Jakir Patel" , "Branch" : "Computer Engineering" , "Roll" : "3355" , "Comment" : "PICT is good college."}
{ "_id" : { "$oid" : "541bff0f532a2e5b3a4ff99e"} , "Name" : "Riya Shaikh" , "Branch" : "Computer Engineering" , "Roll" : "2568" , "Comment" : "All resources are available in PICT"}
{ "_id" : { "$oid" : "541bff11532a2e5b3a4ff99f"} , "Name" : "Madina Kalideva" , "Branch" : "Information Technology" , "Roll" : "5897" , "Comment" : "Its quite good."}
{ "_id" : { "$oid" : "541c00fa532a2e5b3a4ff9a0"} , "Name" : "Madina Kalideva" , "Branch" : "Information Technology" , "Roll" : "5897" , "Comment" : "Its quite good."}
{ "_id" : { "$oid" : "541c00fc532a2e5b3a4ff9a1"} , "Name" : "Riya Shaikh" , "Branch" : "Computer Engineering" , "Roll" : "2568" , "Comment" : "All resources are available in PICT"}
{ "_id" : { "$oid" : "541c00fd532a2e5b3a4ff9a2"} , "Name" : "Jakir Patel" , "Branch" : "Computer Engineering" , "Roll" : "3355" , "Comment" : "PICT is good college."}

 INDEX INFORMATION:
[0]{ "v" : 1 , "key" : { "_id" : 1} , "name" : "_id_" , "ns" : "te3355db.Feedback"}
[1]{ "v" : 1 , "key" : { "Roll" : 1 , "Name" : -1} , "name" : "Roll_1_Name_-1" , "ns" : "te3355db.Feedback"}
----------------------------------------
Collection is created suucessfully!!!
----------------------------------------
{ "_id" : { "$oid" : "541c011b8314e16dfe8fcc64"} , "Category" : "Animation and motion making"}
{ "_id" : { "$oid" : "541c013f83144ecf2554d9a2"} , "Category" : "Animation and motion making"}
{ "_id" : { "$oid" : "541c01568314221e117b5bc7"} , "Category" : "Animation and motion making"}
{ "_id" : { "$oid" : "541c0158831419be5f1a0855"} , "TITLE" : "MOVIE" , "Name" : "Avtar" , "Category" : "Motion Making" , "Rating" : "8.5"}
Document updated successfully
Updated document.
----------------------------------------
{ "_id" : { "$oid" : "541c011b8314e16dfe8fcc64"} , "Category" : "Animation and motion making"}
{ "_id" : { "$oid" : "541c013f83144ecf2554d9a2"} , "Category" : "Animation and motion making"}
{ "_id" : { "$oid" : "541c01568314221e117b5bc7"} , "Category" : "Animation and motion making"}
{ "_id" : { "$oid" : "541c0158831419be5f1a0855"} , "Category" : "Animation and motion making"}
 INSERTED DOCUMENT IN Feedback COLLECTION AFTER REMOVING FIRST DOCUMENT
{ "_id" : { "$oid" : "541bff0f532a2e5b3a4ff99e"} , "Name" : "Riya Shaikh" , "Branch" : "Computer Engineering" , "Roll" : "2568" , "Comment" : "All resources are available in PICT"}
{ "_id" : { "$oid" : "541bff11532a2e5b3a4ff99f"} , "Name" : "Madina Kalideva" , "Branch" : "Information Technology" , "Roll" : "5897" , "Comment" : "Its quite good."}
{ "_id" : { "$oid" : "541c00fa532a2e5b3a4ff9a0"} , "Name" : "Madina Kalideva" , "Branch" : "Information Technology" , "Roll" : "5897" , "Comment" : "Its quite good."}
{ "_id" : { "$oid" : "541c00fc532a2e5b3a4ff9a1"} , "Name" : "Riya Shaikh" , "Branch" : "Computer Engineering" , "Roll" : "2568" , "Comment" : "All resources are available in PICT"}
{ "_id" : { "$oid" : "541c00fd532a2e5b3a4ff9a2"} , "Name" : "Jakir Patel" , "Branch" : "Computer Engineering" , "Roll" : "3355" , "Comment" : "PICT is good college."}

No comments:

Post a Comment

Home Java Python PHP