You have more than few options for that. In order of increasing scalability/performance:
Don’t do anything special, store the hashtags within the post body and do either one of regex or text search on the post body
Store the hashtags within the post body, and also use one of the search packages that makes use of elastic search to search for posts containing a given hashtag
Store the hashtags in an array along with the post document, and just do a standard find on the hashtag
Store a separate collection of (unique) hashtags and their related post id’s and insert/update within afterinsert/afterupdate/afterremove hooks on the post collection (or if you don’t want realtime, you could create a job that updates the hashtag colllection very N mins/hrs)
Use one of the redis packages to store key value pairs of hashtag/postid and query on those
I’m sure you can refine these further, even come up with other strategies.
Meteor.methods({
addTagToArticle: function(arg) {
var tag = Tags.findOne(arg.tag_id);
if (tag) {
var article = Articles.findOne(arg.article);
if (article) {
article = article.tags;
var hasTag = article.indexOf(tag._id); // returns index or if not present, -1
if (hasTag < 0) {
Articles.update(arg.article, {$push: {tags: tag._id}}, function(err, res) {
if (err) {
throw new Meteor.Error('Something went wrong. ' + err);
} else {
Articles.update(article._id {$push: {tagsFull: tag.name}}); // used for EasySearch
Tags.update(tag._id, {$inc: {uses: 1}});
}
});
} else {throw new Meteor.Error('Keyword already belongs to this article.');}
} else {throw new Meteor.Error('Article does not exist');}
} else {throw new Meteor.Error('Keyword does not exist');}
}
I don’t understand your question.
You just need the field if you are using EasySearch (denormalize data).
Otherwise you could not search for tags.
I’m pushing the full tag name into the array field in step 3)