List of related tags for blog posts stored in MongoDB

I am trying to figure out how I can get a list of related tags from blog posts stored in MongoDB.

Data structure

{ title: "Post #1", tags: { "news", "politics" } }, { title: "Post #2", tags: { "news", "entertainment" } }, { title: "Post #3", tags: { "entertainment", "music", "theatre" } }, { title: "Post #4", tags: { "entertainment", "music", "concerts" } } 

Desired Result

If I want to get a list of tags related to "entertainment", it asks for messages to find similar tags. Similar tags are those that are also used when the message is marked as "entertainment."

I would like to get the following result:

 Tag Count ======== ====== music 2 (because there are 2 posts tagged with music + entertainment) concert 1 theatre 1 news 1 

Is there a way to get as close to this as possible? The closest I managed to get is to use db.posts.find({tags: "entertainment"}); and then loop through and build these values ​​outside of MongoDb. I am looking for a more efficient way.

+4
source share
2 answers

You can click on the work on the write side to support fast reading. Suppose you are trying to add new_tag to a message that already has some_list_of_tags . The following code will create a collection with the required values:

 for old_tag in some_list_of_tags: db.related_tags.update({'_id':new_tag}, {'$inc':{'counts.'+old_tag:1}}, upsert=True) db.related_tags.update({'_id':old_tag}, {'$inc':{'counts.'+new_tag:1}}, upsert=True) 

Then, to get results for 'entertainment, just do:

 db.related_tags.find({'_id': 'entertainment'}) 

You can use the findAndModify command to atomically add a tag to a message and fetch all existing tags:

 old_tags = db.posts.findAndModify({query: {_id: ID}, update: {$addToSet: {tags: new_tag}}, fields: {tags: 1} })['tags'] 
+1
source

You will not find it. MongoDB has very limited (but very efficient) query capabilities. For something like, you will need a map / reduce, but since MongoDB M / R is single-threaded today, and the JS engine is not the fastest, you may get a better solution.

0
source

Source: https://habr.com/ru/post/1334461/


All Articles