Attribute pattern and index wildcards

The main reason provided in the attribute pattern lesson for using this pattern is to avoid creating multiple indexes when searching on those values.

With the introduction of index wildcard in MongoDB 4.2, is the attribute pattern still relevant? (other than the cases when the index wildcard can’t be used)

Hey @Martin_44478

Did you happen to see the lecture notes for that video?

With the release of the Wildcard Index functionality in MongoDB 4.2, some use cases of the Attribute Pattern can be replaced by this new index type.

To learn more about the Wildcard Index , please consult our documentation or watch the lessons in our MongoDB 4.2 online course, M042.

Looks like they say there would still be some use cases. I am not too sure on an example atm. Just wanted to bring the notes to your attention.

1 Like

I hadn’t seen them, good catch. I guess this answers my question, although it could be interesting to know when the attribute pattern is a better idea than the wildcard index.

Truthfully, I think you had the nice catch since you were already thinking about Wildcard Indexes without reading it. :clap:

The Wildcard Index should be the preferred choice over the Attribute Pattern if you are using MongoDB 4.2.

That said, there are some relationships that can be “enhanced” or “qualified” by using the Attribute Pattern.
For example, let’s say that I have the following:

{
  "veteran_discount": 18
  "price": 20.99
}

And I decided to apply the Attribute Pattern, resulting in:

{
  attributes: [
    { "k": "veteran_discount", "v": 18 },
    { "k": "price", "v": 20.99 },
  ]
}

I may decide that some relationships may need more information, for example keeping track of the currency for the price, resulting in:

{
  attributes: [
    { "k": "veteran_discount", "v": 18 },
    { "k": "price", "currency": "euros", "v": 20.99 },
  ]
}

Again, the Wildcard Index simplifies a lot and let you keep the documents in a more natural form, but keep in the back of your mind what the Attribute Pattern could do for you.

Regards,
Daniel

3 Likes