Fast related tag search in MongoDB
Pergunta
I've got two document sets:
Wikis and WikiTags. Since i want flexible editing of tag names I don't want to embed tag itself into wiki document. So, I store a list of wiki_tag_ids inside wiki document.
I wonder what is the best way to find related tags using this schema. By related tags I mean tags that exist in other wikis tagged with selected tags.
May be I should store related tags in WikiTag document?
Solução
I suggest you should store to store WikiTag in Wiki document. Mongodb allow easy update, delete single document from nested collection, thats mean 'flexible editing of tag names'.
Collection like this:
wikis
{
_id,
wikiTags {_id, name, ...},
...
}
So, for example if you want update nested WikiTag name with id = SomeTagId
you can:
db.wikis.update( {'wikiTags.id':SomeTagId},
{$set:{'wikiTags.$.name':"New Tag Name"}},
false,
true )
If yoy want delete item from nested array you should use $unset, add new item: $push, $addToSet
So, i guess now you see that any operation with nested array can be done easy. And if performance is an issue -- use embedding.
Hope this helps.