Search⌘ K
AI Features

Write Query: Create a Compound Index

To address slow query performance in the orders collection, a compound index should be created on the fields customerId (ascending) and orderDate (descending). This index will optimize queries that filter by customer and sort by the most recent orders, reducing the need for MongoDB to scan and sort large data sets. The existing single-field index on customerId is insufficient, as it does not account for the orderDate sorting requirement.

We'll cover the following...

Question

Given the following sample documents from the orders collection:

[
  {
    "_id": "3a758b14e7e292cf190b1ac5",
    "customerId": "ee3c04494293e51a48af88d1",
    "items": [
      "External Hard Drive",
      "USB Hub"
    ],
    "total": 1922,
    "status": "Processing",
   
...