How do blog tags work?

Hi Everyone,

I’m new to Node JS and MongoDB and I’ve always had a burning question: How does the tagging work for blog posts?

As I slowly learn about MongoDB, I am trying to comprehend how this tagging system work, but can’t seem to understand a few things.

I understand that there would a many-to-many relationship between the blog posts and the tags, but from what I know about the MongoDB documents’ 16Mb limit, I can’t seem to understand how potentially millions of blog posts or their IDs could be placed under one tag document.

As I said I am still new to MongoDB and stilling getting a handle of schemas and the process of embedding values from one collection to another, so if anyone knows about this topic, I would appreciate even a high-level explanation or an example schema that might highlight how the 16Mb limit isn’t exceed.

Thanks for the help!

Hi @Sanskar_Patel ,

Thats an interesting observation. You are correct that creating a one document that holds pointer (ids) to all the tagged posts with this tag is a bad design that can easily reach 16mb limit.

However, MongoDB allows indexing arrays. This mean that each post can have an array of tags :

{
_id : "post1",
title : "my post",
tags : ["blogpost", "goodpost"]
...
},
{
_id : "post2",
title : "new post",
tags : ["blogpost", "news"]
...
},

Using this design we will be able to show tags per post easily, but also index {tags : 1} and query by tag:

db.posts.find({tags : "news"})

Hope that many to many relationship make sense.

Thanks
Pavel

1 Like

Thanks for the explanation @Pavel_Duchovny!

However, for example, let’s say that there are thousands of blog posts with the tag ‘dog’ in them, how would querying for all the posts with the tag ‘dog’ be achieved?

How would track all the posts with this tag?
Would you have a separate collection for ‘posts’ and one for ‘tags’, and have a many to many relationship?

Hi @Sanskar_Patel ,

I will have one collection name posts and will have a field of “tags” this field will be an array.

This means that it can hold one or many different tags.

When I want to find all post documents that have a tag of “dog” I just need to search it in the “tags” field of each post:

db.posts.find({"tags" : "dog"});

Since I can also index arrays I can index the “tags” field and each value will now be indexed therefore ({"tags" : "dog"}) will be fast even if it return 10k posts out of total of 10m blogs.

I will maintain a “tags” collection only if the application has a defined set of tags the user needs to choose from. But I will then after showing the list of tags will duplicate each tag into the corresponding post document “tags” array as the user tags the post:

db.post.updateOne({_id : "post1"}, {"$addToSet" : { tags : "dog" }});

See the following examples as well:

Ty
Pavel

1 Like