我想弄清楚如何從存儲在MongoDB中的博客文章中獲取相關標籤的列表。列出存儲在MongoDB中的博客文章的相關標籤
數據結構
{
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" }
}
所需的結果
如果我想要得到的與「娛樂」的標籤列表,它查詢職位,以找到類似的標籤。類似的標籤是當帖子被標記爲「娛樂」時也使用的標籤。
我希望能夠得到以下結果:
Tag Count
======== ======
music 2 (because there are 2 posts tagged with music + entertainment)
concert 1
theatre 1
news 1
有沒有辦法把它作爲接近越好?我能夠得到的最接近的是使用db.posts.find({tags: "entertainment"});
,然後在MongoDb之外循環並構建這些值。我正在尋找更有效的方法。
我沒有想到該選項。好主意。 – Trevor 2011-01-07 19:52:12