Data modeling - attribute pattern

Hi,
Why does the attribute pattern it reduce the number of indexes?
We will anyhow need to store index data for each element in the attribute array?

Thanks,
Feigy Haim

With attribute pattern, you need 1 index, { attributes.k : 1 , attributes.v : 1 }.

Without attribute pattern you would need 1 index per attribute and more if you want to query combination of attributes.

For example, you may have documents without attribute pattern that looks like

{ _id : 0 , color : blue , size : 1 }
{ _id : 1 , color : red , size : 1 , material : platic }

Which would give using the attribute pattern

{ _id : 0 ,
  attributes: [
    { k : color , v: blue } ,
    { k : size , v : 1 }
  ]
} ,
{ _id : 1 ,
  attributes: [
    { k : color , v: red } ,
    { k : size , v : 1 } ,
    { k : material , v : platic }
  ]
}

In the non-attribute pattern, you need at least 3 indexes:

{ color : 1 }
{ size : 1 }
{ material : 1 }

With the attribute pattern you need 1 :

{
  "attributes.k" : 1 , "attributes.v" : 1
}
2 Likes

I undestand this, but index on an array will store data for each element in the array?

Yes it will. The meaning of

means that the number of indexes will be less. It does not mean that there is less values in the indexes or that the indexes will take more spaces.

1 Like

Thanks Steeve,
So it does not save RAM compared to having index on each field, if I understand correctly?
Incase the attribute can have 200 keys and we can have millions of documents, can we index this attribute? or can this cause us to run out of RAM, and better not to index this?

Of course it can. Your hardware must be appropriate for the use-case.

If querying the attribute is a frequent use-case, it has to be indexed. Your hardware must be appropriate for the use-case.

It may or it may not is it use-case specific. If you are stressed with the amount of RAM, you should take the time to compare the different approaches. There is no solution that fits all. It is really easy to add more RAM. Is it easier and faster than trying to came up with the perfect solution.

Continuous improvement is better than delayed perfection. – Mark Twain

Make it work correctly then make it work fast.

A excellent idea that can be used with the attribute pattern is Atlas Search to return all the applicable filters for a given search query without specifying - #2 by Erik_Hatcher.

1 Like