Question 7
Given the following indexes:
- { categories: 1, price: 1 }
- { in_stock: 1, price: 1, name: 1 }
The following documents:
- { price: 2.99, name: "Soap", in_stock: true, categories: ['Beauty', 'Personal Care'] }
- { price: 7.99, name: "Knife", in_stock: false, categories: ['Outdoors'] }
And the following queries:
- db.products.find({ in_stock: true, price: { $gt: 1, $lt: 5 } }).sort({ name: 1 })
- db.products.find({ in_stock: true })
- db.products.find({ categories: 'Beauty' }).sort({ price: 1 })
Answer:
- Index #1 would provide a sort to query #3. - True
- Index #2 properly uses the equality, sort, range rule for query #1. - False - If we were to build an index for query #1 using the equality, sort, range rule, then the index would be: { in_stock: 1, name: 1, price: 1 }.
- There would be a total of 4 index keys created across all of these documents and indexes. - False - As there would be 5 total index keys:
- { categories: true, price: 2.99 }
- { categories: false: price: 7.99 }
- { in_stock: true, price: 2.99, categories: 'Beauty' }
- { in_stock: true, price: 2.99, categories: 'Personal Care' }
- { in_stock: false, price: 7.99, categories: 'Outdoors'}
- Index #2 can be used by both query #1 and #2. - True
Comments
Post a Comment