I want to be able to add up to 10 tags to a record in a database (MongoDB), but I don't want to add 10 columns with the corresponding indexes for each of them. So I decided to add a unique amount of these tags.
eg. (with 6 tags)
|------------|
|value | tag |
|------------|
|1 |a |
|2 |b |
|4 |c |
|8 |d |
|16 |e |
|32 |f |
|------------|
eg.
a + b = 3
b + c + d = 14
Then I save only the sum of the value in Mongo.
These combinations are always unique, and I can "restore" them back to tags when retrieving from persistent storage using iteration.
int tagSum
for each (tag in tagCollection.OrderDescending)
{
if (tagSum >= (int)tag)
{
TagProperty.Add(targetAge);
tagSum -= (int)tag;
}
}
However, my problem is that I thought there should be a mathematical formula that I could use to request a specific tag, for example. find "c tag" by passing the value 4. I am either mistaken or cannot find it.
Multikeys Mongo, , 1 10