Question 7

Given the following indexes:
  1. { categories: 1, price: 1 }
  2. { in_stock: 1, price: 1, name: 1 }
The following documents:
  1. { price: 2.99, name: "Soap", in_stock: true, categories: ['Beauty', 'Personal Care'] }
  2. { price: 7.99, name: "Knife", in_stock: false, categories: ['Outdoors'] }
And the following queries:
  1. db.products.find({ in_stock: true, price: { $gt: 1, $lt: 5 } }).sort({ name: 1 })
  2. db.products.find({ in_stock: true })
  3. db.products.find({ categories: 'Beauty' }).sort({ price: 1 })
Which of following is/are true?

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'}
           The additional index keys are due to the multikey index on categories.
  • Index #2 can be used by both query #1 and #2. - True


Comments

Popular posts from this blog

M201 MongoDB Performance - Labs & Final Exam Q&A

Lab 2.1