Wednesday, October 29, 2014

Indexing and querying with MongoDB using suitable example.

Problem Statement:
Indexing and querying with MongoDB using suitable example.

TERMINAL:

[db@localhost ~]$ cd /home/pict/mongodb-linux-x86_64-2.6.3/bin
[db@localhost bin]$ ./mongo 192.168.5.122/db -u root -p root
MongoDB shell version: 2.6.3
connecting to: 192.168.5.122/db
> db.createCollection("Student");
{ "ok" : 1 }
> db.Student.insert({"_id":1,"roll_no":3205,"Name":'Amol Modi',"dmsa marks":"90","array":["name","Amol Modi","dmsa marks"]});
WriteResult({ "nInserted" : 1 })
> db.Student.insert({"_id":2,"roll_no":3206,"Name":'Anjana Pradeep',"dmsa marks":"75","array":["name","Anjana Pradeep","dmsa marks"]});
WriteResult({ "nInserted" : 1 })
> db.Student.insert({"_id":3,"roll_no":3228,"Name":'Rohan Jain',"dmsa marks":"85","array":["name","Rohan Jain","dmsa marks"]});
WriteResult({ "nInserted" : 1 })
> db.Student.insert({"_id":4,"roll_no":3254,"Name":'Payal Rathod',"dmsa marks":"70","array":["name","Payal Rathod","dmsa marks"]});
WriteResult({ "nInserted" : 1 })
> db.Student.find().pretty();
{
"_id" : 1,
"roll_no" : 3205,
"Name" : "Amol Modi",
"dmsa marks" : "90",
"array" : [
"name",
"Amol Modi",
"dmsa marks"
]
}
{
"_id" : 2,
"roll_no" : 3206,
"Name" : "Anjana Pradeep",
"dmsa marks" : "75",
"array" : [
"name",
"Anjana Pradeep",
"dmsa marks"
]
}
{
"_id" : 3,
"roll_no" : 3228,
"Name" : "Rohan Jain",
"dmsa marks" : "85",
"array" : [
"name",
"Rohan Jain",
"dmsa marks"
]
}
{
"_id" : 4,
"roll_no" : 3254,
"Name" : "Payal Rathod",
"dmsa marks" : "70",
"array" : [
"name",
"Payal Rathod",
"dmsa marks"
]
}
> db.Student.ensureIndex({"name":1}); //simple index
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.Student.ensureIndex({"array":1});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
> db.Student.ensureIndex({"_id":-1,"name":1}); //compound index
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
> db.Student.ensureIndex({"roll_no":1},{unique:true}); //unique index
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 4,
"numIndexesAfter" : 5,
"ok" : 1
}
> db.Student.find({"array":"name"}).pretty(); //querying
{
"_id" : 1,
"roll_no" : 3205,
"Name" : "Amol Modi",
"dmsa marks" : "90",
"array" : [
"name",
"Amol Modi",
"dmsa marks"
]
}
{
"_id" : 2,
"roll_no" : 3206,
"Name" : "Anjana Pradeep",
"dmsa marks" : "75",
"array" : [
"name",
"Anjana Pradeep",
"dmsa marks"
]
}
{
"_id" : 3,
"roll_no" : 3228,
"Name" : "Rohan Jain",
"dmsa marks" : "85",
"array" : [
"name",
"Rohan Jain",
"dmsa marks"
]
}
{
"_id" : 4,
"roll_no" : 3254,
"Name" : "Payal Rathod",
"dmsa marks" : "70",
"array" : [
"name",
"Payal Rathod",
"dmsa marks"
]
}
> db.Student.find({},{roll_no:1}).pretty();
{ "_id" : 1, "roll_no" : 3205 }
{ "_id" : 2, "roll_no" : 3206 }
{ "_id" : 3, "roll_no" : 3228 }
{ "_id" : 4, "roll_no" : 3254 }
> db.Student.find({roll_no:{$gt:3206}}).pretty();
{
"_id" : 3,
"roll_no" : 3228,
"Name" : "Rohan Jain",
"dmsa marks" : "85",
"array" : [
"name",
"Rohan Jain",
"dmsa marks"
]
}
{
"_id" : 4,
"roll_no" : 3254,
"Name" : "Payal Rathod",
"dmsa marks" : "70",
"array" : [
"name",
"Payal Rathod",
"dmsa marks"
]
}

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...