-2

index:

{
  A: 1,
  B: 2
}
{
  A: 1,
  C: 2
}

Query:

user.aggregate([{
    $match: {
        A: "house",
        $or: [ 
          { B: "car" },
          { C: "boat" },
        ]
      } 
  }
])

In order to check the $or condition, will mongodb make use of both indexes?

EDITED:

According to the documentation on mongo's site, multiple single field can indexes can be used in the same query. However, in my question, both indexes are compound indexes.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • 2
    Similar to other questions, an explain plan should answer the question. Seems you already have the pieces in place to gather it. Also the [documentation on the topic](https://www.mongodb.com/docs/manual/reference/operator/query/or/#-or-clauses-and-indexes) is helpful and relevant here – user20042973 Aug 16 '23 at 00:37
  • 2
    As usual, it is trivially testable in [Mongo playground](https://mongoplayground.net/p/3fAwk2-fgJj) – ray Aug 16 '23 at 00:42
  • According to the doc from @user20042973, it should use both indexes, but according to the playground, it only used 1, unless I am misinterpretation. The documentation only commented on single field indexes. – Bear Bile Farming is Torture Aug 16 '23 at 00:54
  • Related to [your earlier question](https://stackoverflow.com/a/76910986/3027266), I don't think there is a general answer. – Wernfried Domscheit Aug 16 '23 at 06:27

0 Answers0