mongodbmongodb-querynosqlcompound-index

MongoDB deciding WinningPlan and RejectedPlans


I have a mongo collection as below:

{
    "_id" : ObjectId("6225a7b8d06fdc0abe145634"),
    "name" : "Max",
    "age" : 28,
    "salary" : 3000
}
{
    "_id" : ObjectId("6225a7b8d06fdc0abe145635"),
    "name" : "Manu",
    "age" : 25,
    "salary" : 4000
}

I have three indexes for this collection:

  1. The default _id index
  2. Standalone name_1 index
  3. Compound index of age_1_name_1

When I executed a query like db.customers.explain().find({name: "Max", age: 28}), I was expecting the winningPlan to be the compound index, instead the winning plan was the standalone name_1 index and the rejectedPlan was the compound index.

Can anyone help me understand why is this happening?


Solution

  • Your compound index need to be name_1_age_1 to be possible to be used by the query ...