Meteor:查找具有最高价值的文档

Jea*_*eri 9 javascript meteor

我想在具有特定属性的最高值的集合中找到一个文档.

假设文档具有以下结构

{
  _id: 'jswehfkwefkjw',
  price: 10,
  ....
}
Run Code Online (Sandbox Code Playgroud)

如何选择价格最高的文档?

我找到了这个

transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first();
Run Code Online (Sandbox Code Playgroud)

但我无法将其转化为流星:(我现在看起来像这样

Articles.find({}, {sort: {price: -1}}).fetch();
Run Code Online (Sandbox Code Playgroud)

但这不行.有什么建议 ?

And*_*kul 12

我不清楚你的意思是"这不做".

但我建议使用findOne而不是fetch,因为fetch返回一个数组,你想要一个文档.

Articles.findOne({}, {sort: {price: -1}});
Run Code Online (Sandbox Code Playgroud)

要么

Articles.find({},{limit: 1, sort: {price: -1}});
Run Code Online (Sandbox Code Playgroud)

然后使用fetch并使用数组表示法[0]或下划线_.first(数组)获取第一个元素