0% found this document useful (0 votes)
2 views

Mongodb Sample Querying Indexing

The document outlines a series of MongoDB commands for managing a student information system database named 'studentDB'. It includes operations such as creating a database and collection, inserting and querying student records, updating contact information, sorting data, managing indexes, and deleting records. The commands demonstrate various functionalities of MongoDB, including logical and comparison queries, data projection, and database cleanup.

Uploaded by

tvsjaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Mongodb Sample Querying Indexing

The document outlines a series of MongoDB commands for managing a student information system database named 'studentDB'. It includes operations such as creating a database and collection, inserting and querying student records, updating contact information, sorting data, managing indexes, and deleting records. The commands demonstrate various functionalities of MongoDB, including logical and comparison queries, data projection, and database cleanup.

Uploaded by

tvsjaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MongoDB- Sample Usecase – Querying & Indexing

You are working as a database engineer for a student information system that stores student
records in a MongoDB collection named students, with fields such as stud_regno, name, cgpa,
and contact_no. Write MongoDB commands to perform the following operations: create and
select a database, create a collection, insert one and multiple student records, query students based
on cgpa using logical and comparison operators, display all records, project specific fields, update
contact information, sort data by CGPA, create and manage indexes, delete specific or multiple
records, drop the collection, and finally drop the database.

// 1. Use or create the database


> use studentDB
switched to db studentDB

// 2. Create a collection
> db.createCollection("students")
{ "ok" : 1 }

// 3. Insert one student


> db.students.insertOne({
stud_regno: "43110001",
name: "Arun",
cgpa: 8.2,
contact_no: "9999911111"
})
{
"acknowledged" : true,
"insertedId" : ObjectId("...")
}

// 4. Insert multiple students


> db.students.insertMany([
{ stud_regno: "43110002", name: "Beena", cgpa: 5.9, contact_no: "9999922222" },
{ stud_regno: "43110003", name: "Chitra", cgpa: 6.5, contact_no: "9999933333" },
{ stud_regno: "43110004", name: "Dinesh", cgpa: 7.0, contact_no: "9999944444" },
{ stud_regno: "43110005", name: "Esha", cgpa: 5.5, contact_no: "9999955555" }
])
{
"acknowledged" : true,
"insertedIds" : {
"0" : ObjectId("..."),
"1" : ObjectId("..."),
"2" : ObjectId("..."),
"3" : ObjectId("...")
}
}

// 5. Display all records (pretty)


> db.students.find().pretty()
{
"_id" : ObjectId("..."),
"stud_regno" : "43110001",
"name" : "Arun",
"cgpa" : 8.2,
"contact_no" : "9999911111"
}
{
"stud_regno" : "43110002",
"name" : "Beena",
"cgpa" : 5.9,
"contact_no" : "9999922222"
}
...

// 6. Find CGPA > 6.0


> db.students.find({ cgpa: { $gt: 6.0 } }).pretty()
{
"stud_regno" : "43110001", "name" : "Arun", "cgpa" : 8.2, ...
}
{
"stud_regno" : "43110003", "name" : "Chitra", "cgpa" : 6.5, ...
}
{
"stud_regno" : "43110004", "name" : "Dinesh", "cgpa" : 7.0, ...
}

// 7. CGPA < 6.0


> db.students.find({ cgpa: { $lt: 6.0 } }).pretty()
{
"stud_regno" : "43110002", "name" : "Beena", "cgpa" : 5.9, ...
}
{
"stud_regno" : "43110005", "name" : "Esha", "cgpa" : 5.5, ...
}

// 8. CGPA >= 6.0 and <= 7.0


> db.students.find({ cgpa: { $gte: 6.0, $lte: 7.0 } }).pretty()
{
"stud_regno" : "43110003", "name" : "Chitra", "cgpa" : 6.5, ...
}
{
"stud_regno" : "43110004", "name" : "Dinesh", "cgpa" : 7.0, ...
}

// 9. CGPA != 6.0
> db.students.find({ cgpa: { $ne: 6.0 } }).pretty()
// Returns all records except those with cgpa == 6.0

// 10. Project name and cgpa only


> db.students.find({}, { _id: 0, name: 1, cgpa: 1 }).pretty()
{ "name": "Arun", "cgpa": 8.2 }
{ "name": "Beena", "cgpa": 5.9 }
...

// 11. Sort by CGPA descending


> db.students.find().sort({ cgpa: -1 }).pretty()
// Sorted list with Arun (8.2) first

// 12. Update contact number


> db.students.updateOne(
{ stud_regno: "43110003" },
{ $set: { contact_no: "9988776655" } }
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

// 13. Create index


> db.students.createIndex({ stud_regno: 1 })
"stud_regno_1"

// 14. View indexes


> db.students.getIndexes()
[
{ "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", ... },
{ "v" : 2, "key" : { "stud_regno" : 1 }, "name" : "stud_regno_1", ... }
]

// 15. Drop specific index


> db.students.dropIndex({ stud_regno: 1 })

{ "nIndexesWas" : 2, "ok" : 1 }

// 16. Drop all indexes (except _id)


> db.students.dropIndexes()

{ "nIndexesWas" : 1, "msg" : "non-_id indexes dropped", "ok" : 1 }

// 17. Delete one student


> db.students.deleteOne({ stud_regno: "43110005" })

{ "acknowledged" : true, "deletedCount" : 1 }

// 18. Delete students with CGPA < 6.0


> db.students.deleteMany({ cgpa: { $lt: 6.0 } })

{ "acknowledged" : true, "deletedCount" : 1 }

// 19. Delete all records


> db.students.deleteMany({})

{ "acknowledged" : true, "deletedCount" : X }

// 20. Drop the collection


> db.students.drop()
true

// 21. Drop the database


> db.dropDatabase()

{ "ok" : 1 }

You might also like