Advice for different structure on error "cannot index parallel arrays"

i have the following document structure:

{
  "_id": "123",
  "timestamp": 1628632419,
  "myArray": [
    {
      "myNestedArray": [
        {
          "name": "NestedName1",
          "value": "1"
        },
        {
          "name": "NestedName2",
          "value": "2"
        },
        {
          "name": "NestedName3",
          "value": "3"
        }
      ],
      "type": "MyType",
      "name": "MyName",
      "propertyA": "A",
      "propertyB": "B",
      "propertyC": "C",
      "propertyD": "D",
      "propertyE": "E",
    },
    ...
  ]
}

With that, I want to create an index like that:

collection.createIndex({
        'myArray.type': 1,
        'myArray.myNestedArray.name': 1,
        'myArray.myNestedArray.value': 1,
    })

This results in:

cannot index parallel arrays

I read through the documentation and I understand where the problem is. Now my question is, what is a good structure for my document, in order that my indexing is working?

I found the approach to structure from:

{a:[1,2], b:[8,9]}

to:

{ab:[[1,8], [1,9], [2,8], [2,9]]}

But as I see this approach for my situation, the objects under myArray are too complex.

I was also thinking about moving the array indices as own properties like:

"type": "MyType",
"name": "MyName",
"myNestedArray0": {
  "name": "NestedName1",
  "value": "1"
},
"myNestedArray1": {
  "name": "NestedName2",
  "value": "1"
},
...

But this feels wrong and is also not really flexible, furthermore the indexing would be a fix number like:

collection.createIndex({
        'myArray.type': 1,
        'myArray.myNestedArray0.name': 1,
        'myArray.myNestedArray0.value': 1,
        'myArray.myNestedArray1.name': 1,
        'myArray.myNestedArray1.value': 1,
         ...
    })

I’m new to mongoDB and it would be great, if someone experienced could give me an advice on my problem.

Hey @Stephan welcome to the community!

There is no one answer to schema design questions, as this is a very personal choice driven by how the data will be used. This is one of MongoDB’s strengths: you don’t design your schema according to how the data will be stored, but rather according to how the data will be used.

This is easier said than done, when all our trainings with SQL dictate just the opposite :slight_smile:

However to help you along your journey, I would ask:

  • What is the most common query/operation are you doing with the data?
  • How are you expecting the data to grow in the future? Will it grow by adding more documents into the collection (typically good), or will it grow by making existing documents larger (typically not so good)

I think the answers to both questions could provide an initial hint to the direction you need to go.

Also if you’re just starting your MongoDB journey, I would recommend the following resources:

Best regards
Kevin

1 Like