8 A - B
8 A - B
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.
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.
The `tags` field is an array, and MongoDB automatically treats an index on an array field as a
multikey index.
4. Compound Index
Let's create a compound index on `city` and `age` to speed up queries that filter by both city and age.
These indexes will help optimize different types of queries on your `students` collection.
Verify Index
db.students.getIndexes();
8B
db.students.find({}).sort({age: -1}).limit(1);
db.students.find({}).sort({age: -1}).limit(1).explain();
db.students.find({}).sort({age: -1}).limit(1).explain("executionStats");
//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 .
switched to db kannadaMoviesDB
catalog
kannadaMoviesDB> db.catalog.countDocuments()
701
b. Develop queries to illustrate excluding documents with certain words and phrases
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.
Use the $text operator with negation to exclude documents containing specific words or phrases.