在MongoDB中查找集合属性的5个最大值
MongoDB站点不工作 我正在用Meteor构建一个应用程序,我想为集合中的属性选择5个最大值。我怎样才能有效地做到这一点 这个怎么样:在MongoDB中查找集合属性的5个最大值,mongodb,meteor,Mongodb,Meteor,MongoDB站点不工作 我正在用Meteor构建一个应用程序,我想为集合中的属性选择5个最大值。我怎样才能有效地做到这一点 这个怎么样: var items = MyCollection.find({}, { fields : { myAttribute : 1 }, // only fetch this filed sort : { myAttribute : -1 }, // sort by myAttribute (from large to small) limit
var items = MyCollection.find({}, {
fields : { myAttribute : 1 }, // only fetch this filed
sort : { myAttribute : -1 }, // sort by myAttribute (from large to small)
limit : 5 // only take the first 5 elements
}).fetch();
这样,您将收到带有\u id的和myAttribute
的文档。如果您只需要这些值,请执行以下操作:
var values = _.pluck(items, 'myAttribute');