Excessive Document Examination in MongoDB with Compound Index and $geoNear Query

Hello MongoDB Community,

I’m currently working with a collection named “merchants” and I’ve set up a compound index with the following attributes: **(tenant_id(asc), merchant_location(2dsphere))**.

I’m running into an issue when executing a pipeline with a $geoNear query. The pipeline is as follows:

db.merchants.aggregate([
  {
    $geoNear: {
      near: [80.94593574932074, 26.847162163506574],
      distanceField: "user_to_merchant_distance_meters",
      distanceMultiplier: 6371000,
      spherical: true,
      key: "merchant_location",
      query: {
        tenant_id: 1003
      },
    },
  }
]);

The problem is, this pipeline returns 275 documents, but it examines 345 documents.

image

This is puzzling because the tenant_id = 1003 only has 281 records.

I’m not sure why it’s examining more documents than exist for 1003 tenant_id.

I would appreciate any insights or suggestions on why this might be happening and how I can optimize this to reduce the number of documents being examined.

Thank you in advance for your help!

Best, Azam