Assignments Of Day 9: Indexes in MongoDB
Assignment 1: Create a Single Field Index
Problem: Create a collection named books and create an index on the author field.
Solution:
1. Step 1: Create the books collection and insert sample data:
javascript
CopyEdit
db.books.insertMany([
{ title: "Book1", author: "John", price: 300 },
{ title: "Book2", author: "Jane", price: 450 },
{ title: "Book3", author: "John", price: 500 }
]);
2. Step 2: Create an index on the author field:
javascript
CopyEdit
db.books.createIndex({ author: 1 });
3. Step 3: Verify the index creation:
javascript
CopyEdit
db.books.getIndexes();
This will create an index on the author field in the books collection, improving the query performance when searching for books by author.
Assignment 2: Create a Compound Index
Problem: Create a collection named employees and create a compound index on department and salary.
Solution:
1. Step 1: Create the employees collection and insert sample data:
javascript
CopyEdit
db.employees.insertMany([
{ name: "Alice", department: "HR", salary: 4000 },
{ name: "Bob", department: "IT", salary: 6000 },
{ name: "Charlie", department: "HR", salary: 4500 }
]);
2. Step 2: Create a compound index on department and salary:
javascript
CopyEdit
db.employees.createIndex({ department: 1, salary: -1 });
3. Step 3: Verify the compound index:
javascript
CopyEdit
db.employees.getIndexes();
The compound index helps speed up queries that filter by department and sort by salary.
Assignment 3: View Indexes on a Collection
Problem: After creating several indexes on the students collection, list all the indexes using the getIndexes() method.
Solution:
1. Step 1: Create the students collection and insert data:
javascript
CopyEdit
db.students.insertMany([
{ name: "Alice", grade: "A", age: 20 },
{ name: "Bob", grade: "B", age: 22 },
{ name: "Charlie", grade: "A", age: 21 }
]);
2. Step 2: Create some indexes:
javascript
CopyEdit
db.students.createIndex({ name: 1 });
db.students.createIndex({ age: 1 });
3. Step 3: List all indexes in the students collection:
javascript
CopyEdit
db.students.getIndexes();
This will return a list of all indexes in the students collection.
Assignment 4: Drop an Index
Problem: Drop the index created for the name field in the students collection.
Solution:
1. Step 1: Create the index on name:
javascript
CopyEdit
db.students.createIndex({ name: 1 });
2. Step 2: Drop the index:
javascript
CopyEdit
db.students.dropIndex("name_1");
3. Step 3: Verify the index is dropped:
javascript
CopyEdit
db.students.getIndexes();
This will drop the index on the name field and verify that it no longer exists.
Assignment 5: Create a Multikey Index
Problem: Create a collection named orders with a field items as an array. Create an index on the items field.
Solution:
1. Step 1: Create the orders collection and insert sample data:
javascript
CopyEdit
db.orders.insertMany([
{ orderId: 1, items: ["apple", "banana"], total: 15 },
{ orderId: 2, items: ["orange", "grape"], total: 25 }
]);
2. Step 2: Create a multikey index on the items field:
javascript
CopyEdit
db.orders.createIndex({ items: 1 });
3. Step 3: Verify the multikey index:
javascript
CopyEdit
db.orders.getIndexes();
The multikey index will help MongoDB perform efficient queries on array fields like items.
Assignment 6: Create a Text Index
Problem: Create a collection named articles and create a text index on the title field.
Solution:
1. Step 1: Create the articles collection and insert sample data:
javascript
CopyEdit
db.articles.insertMany([
{ title: "MongoDB Basics", content: "Learn MongoDB" },
{ title: "Advanced MongoDB", content: "Master MongoDB" }
]);
2. Step 2: Create a text index on the title field:
javascript
CopyEdit
db.articles.createIndex({ title: "text" });
3. Step 3: Verify the text index:
javascript
CopyEdit
db.articles.getIndexes();
The text index is useful for performing full-text searches on fields.
Assignment 7: Create a Hashed Index
Problem: Create a collection named users and create a hashed index on the username field for sharding purposes.
Solution:
1. Step 1: Create the users collection and insert data:
javascript
CopyEdit
db.users.insertMany([
{ username: "user1", email: "user1@example.com" },
{ username: "user2", email: "user2@example.com" }
]);
2. Step 2: Create a hashed index on the username field:
javascript
CopyEdit
db.users.createIndex({ username: "hashed" });
3. Step 3: Verify the hashed index:
javascript
CopyEdit
db.users.getIndexes();
Hashed indexes are useful for sharding and distributing data evenly across a cluster.
Assignment 8: Create a Geospatial Index
Problem: Create a collection named locations with a field coordinates as a geospatial data type. Create a geospatial index on this field.
Solution:
1. Step 1: Create the locations collection and insert sample data:
javascript
CopyEdit
db.locations.insertMany([
{ name: "Location1", coordinates: { type: "Point", coordinates: [40.7128, -74.0060] } },
{ name: "Location2", coordinates: { type: "Point", coordinates: [34.0522, -118.2437] } }
]);
2. Step 2: Create a geospatial index on the coordinates field:
javascript
CopyEdit
db.locations.createIndex({ coordinates: "2dsphere" });
3. Step 3: Verify the geospatial index:
javascript
CopyEdit
db.locations.getIndexes();
Geospatial indexes are useful for performing location-based queries.
Assignment 9: Create Indexes on Multiple Fields
Problem: Create a collection named products and create indexes on name, price, and category.
Solution:
1. Step 1: Create the products collection and insert sample data:
javascript
CopyEdit
db.products.insertMany([
{ name: "Laptop", price: 1000, category: "Electronics" },
{ name: "Shoes", price: 50, category: "Fashion" }
]);
2. Step 2: Create indexes on name, price, and category:
javascript
CopyEdit
db.products.createIndex({ name: 1 });
db.products.createIndex({ price: -1 });
db.products.createIndex({ category: 1 });
3. Step 3: Verify the indexes:
javascript
CopyEdit
db.products.getIndexes();
This example demonstrates creating multiple indexes on different fields for optimized querying.
Assignment 10: Drop All Indexes
Problem: Drop all indexes except the default _id index in the users collection.
Solution:
1. Step 1: Create the users collection and insert data:
javascript
CopyEdit
db.users.insertMany([
{ username: "user1", email: "user1@example.com" },
{ username: "user2", email: "user2@example.com" }
]);
2. Step 2: Create some indexes:
javascript
CopyEdit
db.users.createIndex({ username: 1 });
db.users.createIndex({ email: 1 });
3. Step 3: Drop all indexes except _id:
javascript
CopyEdit
db.users.dropIndexes();
4. Step 4: Verify the indexes:
javascript
CopyEdit
db.users.getIndexes();
This will remove all custom indexes, leaving only the default _id index.
