I am trying to write a blog engine for myself with node.js /express/mongodb (also a start to learn node.js ). To go a little further than the tutorials on the Internet, I want to add tags support to the blog engine.

I want to do the following things with tags:

Viewers could see all the tags as a tag cloud on a "tag cloud page" Viewers could see the tags that an article has on article list page and single article page Viewers are able to click on a single tag to show the article list What's more, viewers are able to search articles with particluar tags in the SO way: [tag1][tag2] --> /tags/tag1+tag2 --> list of articles that has both tag1 and tag2

In relational database, a post_tag table will be used for this. But how to desgin this in MongoDB?

I have checked MongoDB design - tags

But as efdee comments, the design

db.movies.insert({ name: "The Godfather", director: "Francis Ford Coppola", tags: [ "mafia", "wedding", "violence" ] })

has a problem:

This doesn't seem to actually answer his question. How would you go about getting a distinct list of tags used in the entire movie collection?

That's also my concern: in my design, I need to show a list of all the tags; I also need to know how many articles each tag has. So is there a better way than the design shown above?

My concern with the design above is: if I want to show a list of the tags, the query will go over all the article items in the database. Is there a more efficient way?

Problem courtesy of: coolcfan

Solution

You'd need to create a multi key index on tags to start with.

Then you will be able to find document matching tag using this syntax

db.movies.find({ "tags": { $all : [ /^this/, /^that/ ] }})

Because you're using the ^ (start of string) of the reg ex mongo will still use the index.

To get keyword densities, using the aggregation framework, you could simple get a count.

db.movies.aggregate({ $project: { _id:0, tags: 1}}, { $unwind: "$tags" }, { $group : { _id : "$tags", occur : { $sum : 1 }}})

Sorry formatting difficult from iPad.

You would end up with collection of docs looking like:

{ _id: "mytag", occur: 383 }, { _id: "anothertag", occur: 23 },

Using the aggregate command you get an inline result back, so would be down to the client app (or server) to serialise or cache the result if it's frequently used.

Let me know how you get on with that.

Hth

Sam

Solution courtesy of: sambomartin

Discussion

You could perform your 4 tasks using MapReduce functions. For example, for the list of all tags you'd emit the tag as the key and then in the reduce function you'd count them all up and return the count. That would be the route I'd go down. It may require a little more thought, but it's definitely powerful.

http://cookbook.mongodb.org/patterns/count_tags/

Discussion courtesy of: themanatuf

How would you go about getting a distinct list of tags used in the entire movie collection?

db.movies.distinct("tags")

For efficient queries, I'd probably duplicate data. tags are very unlikely to ever be edited, so I'd put the tags array in the article object, and then also put the tags in a tags collection, and tags has either a count of articles containing that tag, or an array of article ids.

db.movies.insert({ id: 1, name: "The Godfather", director: "Francis Ford Coppola", tags: [ "mafia", "wedding", "violence" ] }); db.tags.insert([ {name: "mafia", movie_count: 1}, {name: "wedding", movie_count: 1}, {name: "violence", movie_count: 1} });

Discussion courtesy of: Kareem

This recipe can be found in it's original form on Stack Over Flow .

本文数据库(综合)相关术语:系统安全软件

tags: tags,tag,count,list,article,db
分页:12
转载请注明
本文标题:Is there any way other than just nesting a tag array in MongoDB for a Blog-Tag s ...
本站链接:https://www.codesec.net/view/584205.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(93)