1

According to this issue: MongoDB compound indexes on array fields

compound index with more than one array field is not allowed.

So let's suppose that the array field is contained. The elements can only come from the enum: ["A", "B", "C"]. And the elements must be unique.

Hence, can I turn this field into an embedded document like so:

{
   field_1: {
        A: boolean
        B: boolean
        C: boolean
  },
  field_2: {
        X: boolean
        Y: boolean
        Z: boolean
  },
}

and then:

db.collection_name.createIndex(
  {"field_1.A": 1}, {"field_1.B": 1}, {"field_1.C": 1},
  {"field_2.X": 1}, {"field_2.Y": 1}, {"field_2.Z": 1}
)
  • 1
    Is the question on this one about how to update documents that currently have arrays for `field_1` and `field_2` into the structure you've shown above? And the boolen value should represent whether or not each of the fields was present originally? – user20042973 Oct 22 '22 at 02:18
  • No. It's about a design change. A schema change to allow for compound indexes. I don't have any data yet. – Bear Bile Farming is Torture Oct 22 '22 at 02:26
  • 1
    Ah, I see the "Hence can I..." part again now when rereading. Could you edit the question to include some details about what type of queries you expect to issue against this data? The generic answer is "_yes, you can have this document structure and have 6 single field indexes_" but I doubt that's going to sufficiently address the underlying question of whether or not it would be good/effective for your use case. We need information about your queries to be able to draft a more informed response – user20042973 Oct 22 '22 at 02:44
  • at this stage, I don't have queries yet. But I expect that to need compound indexes. – Bear Bile Farming is Torture Oct 22 '22 at 03:04

0 Answers0