Complete the code to create an index on the 'name' field in a MongoDB collection.
db.collection.createIndex({ name: [1] })In MongoDB, creating an index with 1 means ascending order, which is the standard way to index a field.
Complete the code to find documents where the 'age' field is greater than 25 using an index.
db.collection.find({ age: { $[1]: 25 } })The $gt operator means 'greater than', which finds documents with 'age' greater than 25.
Fix the error in the index creation code to ensure it uses a valid B-tree index on 'score'.
db.collection.createIndex({ score: [1] })For a B-tree index, use 1 (ascending) or -1 (descending). Here, 1 is correct. 'text' and 'hashed' create different index types.
Fill both blanks to create a compound index on 'lastName' ascending and 'age' descending.
db.collection.createIndex({ lastName: [1], age: [2] })Compound indexes can mix ascending (1) and descending (-1) directions. Here, lastName is ascending, age is descending.
Fill all three blanks to create a filtered index on 'status' equals 'active' and sort by 'createdAt' descending.
db.collection.createIndex({ status: [1], createdAt: [2] }, { partialFilterExpression: { status: [3] } })The index sorts 'status' ascending (1) and 'createdAt' descending (-1). The filter only includes documents where status is 'active'.