我有2个收藏,文章,作者。一篇文章可以有0到N个作者。文章可以是许多article_types之一(article_type是一个字符串)。
第 ... field:article_type:type =>串 ... 拥有并属于许多人:作者 ...
作者 field:author_name:type =>串 ....
使用Mongoid获取每个作者属于特定文章类型的文章数量的最佳方法是什么。例如,输出应该是哈希:
“约翰史密斯”:2 '玛丽琼斯':10 '汤姆佩蒂':22谢谢!
答案 0 :(得分:1)
您可能需要使用map reduce http://mongoid.org/en/mongoid/docs/querying.html#map_reduce
答案 1 :(得分:1)
建议使用聚合框架,因为它可以比map / reduce快5-10倍。 MongoDB文档位于http://docs.mongodb.org/manual/core/aggregation/但是要从Mongoid访问它,您必须使用Article.collection.aggregate或Author.collection.aggregate深入到Moped级别。参考Aggregate with Mongoid