0% found this document useful (0 votes)
5 views6 pages

8 A - B

The document outlines the insertion of multiple student records into a MongoDB collection, along with the creation of various types of indexes to optimize query performance, including unique, sparse, multikey, and compound indexes. It also describes methods to analyze query performance before and after index creation, specifically focusing on finding the student with the highest age. Additionally, it mentions steps for creating a catalog collection and performing text search queries while excluding certain documents based on specified words or phrases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

8 A - B

The document outlines the insertion of multiple student records into a MongoDB collection, along with the creation of various types of indexes to optimize query performance, including unique, sparse, multikey, and compound indexes. It also describes methods to analyze query performance before and after index creation, specifically focusing on finding the student with the highest age. Additionally, it mentions steps for creating a catalog collection and performing text search queries while excluding certain documents based on specified words or phrases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

db.students.

insertMany([ { "studentID": 1, "name": "Student1", "age": 22, "city": "New York", "tags":
["tag1", "tag2", "tag3"] }, { "studentID": 2, "name": "Student2", "age": 25, "city": "San Francisco",
"tags": ["tag1", "tag2", "tag3"] }, { "studentID": 3, "name": "Student3", "age": 30, "city": "Los
Angeles", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 4, "name": "Student4", "age": 35, "city":
"Chicago", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 5, "name": "Student5", "age": 40, "city":
"New York", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 6, "name": "Student6", "age": 45, "city":
"San Francisco", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 7, "name": "Student7", "age": 50,
"city": "Los Angeles", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 8, "name": "Student8", "age":
20, "city": "Chicago", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 9, "name": "Student9", "age":
28, "city": "New York", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 10, "name": "Student10",
"age": 32, "city": "San Francisco", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 11, "name":
"Student11", "age": 26, "city": "Los Angeles", "tags": ["tag2", "tag3"] }, { "studentID": 12, "name":
"Student12", "age": 33, "city": "Chicago", "tags": ["tag1", "tag3"] }, { "studentID": 13, "name":
"Student13", "age": 27, "city": "New York", "tags": ["tag1", "tag2"] }, { "studentID": 14, "name":
"Student14", "age": 31, "city": "San Francisco", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 15,
"name": "Student15", "age": 36, "city": "Los Angeles", "tags": ["tag2", "tag3"] }, { "studentID": 16,
"name": "Student16", "age": 38, "city": "Chicago", "tags": ["tag1", "tag2"] }, { "studentID": 17,
"name": "Student17", "age": 29, "city": "New York", "tags": ["tag1", "tag3"] }, { "studentID": 18,
"name": "Student18", "age": 34, "city": "San Francisco", "tags": ["tag1", "tag2", "tag3"] },
{ "studentID": 19, "name": "Student19", "age": 23, "city": "Los Angeles", "tags": ["tag1", "tag2"] },
{ "studentID": 20, "name": "Student20", "age": 39, "city": "Chicago", "tags": ["tag1", "tag2", "tag3"] }]
);

1. Unique Index

Let's create a unique index on the `studentID` field to ensure that each studentID is unique across
the collection.

db.students.createIndex({ studentID: 1 }, { unique: true });

2. Sparse Index

Let's create a sparse index on the `city` field. This means the index will only include documents that
have the `city` field.

db.students.createIndex({ city: 1 }, { sparse: true });


3. Multikey Index

The `tags` field is an array, and MongoDB automatically treats an index on an array field as a
multikey index.

db.students.createIndex({ tags: 1 });

4. Compound Index

Let's create a compound index on `city` and `age` to speed up queries that filter by both city and age.

db.students.createIndex({ city: 1, age: 1 });

These indexes will help optimize different types of queries on your `students` collection.

Verify Index

db.students.getIndexes();

8B

FIND THE SLOW QUERY WITHOUT CREATING ANY INDEX

// Find the student with the highest age

db.students.find({}).sort({age: -1}).limit(1);

// Explain the query plan

db.students.find({}).sort({age: -1}).limit(1).explain();

// Explain the query with execution statistics

db.students.find({}).sort({age: -1}).limit(1).explain("executionStats");

//Now have the total doc scanned


db.students.find({}).sort({age:1}).limit(1).explain("executionStats").executionStats.totalDocsExamined;

//now time taken for doc scanned


db.students.find({}).sort({age: -1}).limit(1).explain("executionStats").executionStats.executionTimeMillis;

NOW CREATE AN INDEX ON age Field

db.students.createIndex({ age: 1 });

Repeat the 2 commands like

//Now have the total doc scanned after creation of index on age field
db.students.find({}).sort({age:1}).limit(1).explain("executionStats").executionStats.totalDocsExamined;

//now time taken for doc scanned after creation of index on age field

db.students.find({}).sort({age: -1}).limit(1).explain("executionStats").executionStats.executionTimeMillis;

9a .

test> use kannadaMoviesDB

switched to db kannadaMoviesDB

kannadaMoviesDB> show collections

catalog

kannadaMoviesDB> db.catalog.countDocuments()

701

Step 2: Create a Text Index

Step 3: Perform a Text Search Query


To find “raju”
Step 4: Perform a Text Search Query for a phrase

b. Develop queries to illustrate excluding documents with certain words and phrases

Step 1: Create a catalog collection

For this we will use the same catalog collection from our previous example. You can follow the same steps as
earlier to create the collection.

Step 2: Perform Queries to Exclude Documents

Use the $text operator with negation to exclude documents containing specific words or phrases.

Example 1: Exclude Documents Containing the Word “action”

Example 2: Exclude Documents Containing the Phrase “da maga”

You might also like