Fixed length array vs fields

Hello everyone,

I have a document that can be flagged with different tags. These tags are limited in number, meaning that a document can have at most 9 tags. Over those tags, I run queries like: Give me all documents that contains exactly the given tags. The question is, which of the two following approaches would be more performant in terms of index size and speed:

  1. Array containing the code of the tag (a list of integer) and run the queries like:
collection().find({
    $and: [
        {"tags": {$in: [1, 2]}},
        {"tags": {$size: 2}}
    ]
})
  1. Use a field for each possible tag (the tags will never increase in number) and run queries like:
collection().find({
    $and: [
        {"tag1": true},
        {"tag3": true},
        ....
    ]
})

Thank you

Hi @Green ,

It sounds like for tagging its more natural to use an array with tags values.

Thats a common pattern with MongoDB . Obviously index the array field and any combined fields in your queries…

Now to verify that tags 1,2 are the only tags in the query you need to run something like that:

db.collection.find( { tags: { $all: [1,2 ]} , tags : { $size : 2 }} )

With $all and $size you can verify that all tags exist in the same document and the size is 2 so they are the only tags.

See this docs for more:

Thanks
Pavel

Hello @Pavel_Duchovny ,
thank you for your answer, this is what I did!

Thank you!

1 Like