2
我正在使用MongoDB和10Gen node.js驱动程序。获取MongoDB文档中嵌入式集合的计数
我在MongoDB的集合,它具有类似于此文档:
{
_id: ObjectId("500310affdc47af710000001"),
name:'something',
tags:['apple','fruit','red'],
created: Date(1342378221351)
}
我想获得的就是看着所有的文件和集合中获得所有文档的所有标签的重复计数。我试过组功能,得到了这个:
> db.photos.group(
... {cond:{}
... ,key:{tags:true}
... ,initial:{count:0}
... ,reduce:function(doc,out){out.count++}
... ,finalize:function(out){}});
[
{
"tags" : null,
"count" : 35
},
{
"tags" : [
"#strawberry",
"#friutpicture"
],
"count" : 1
}
]
这是不对的。我可以得到清晰的没有像这样的计数:
> db.photos.distinct('tags')
[
"#friutpicture",
"#strawberry",
"#factory",
"#wallpaper",
"#bummber",
"#test",
"#fire",
"#watermark"
]
但我需要计数。
我困在2.0.6中。我跟着链接,发现这个食谱:[这里](http://cookbook.mongodb.org/patterns/count_tags/),正是我需要感谢! – 2012-07-15 22:28:22