在MongoDB中排序后获取对象的位置

Get position of object after sort in MongoDB

本文关键字:取对象 位置 获取 MongoDB 排序      更新时间:2023-09-26

我开发了一个Web应用程序,我正在使用MongoDB来存储数据。我能够对数据进行排序并正确显示,但我想在使用 Mongo 排序后获取对象的位置。到目前为止,我有以下代码:

Websites.find({_id: {$lte: website_id}}, {sort:{upvotes: -1}}).count();

它不太管用。它返回某种位置,但与sort:{upvotes: -1}无关。

那么,问题是,我怎样才能1回到最赞成票的,2回到第二个,等等?

这就是你如何做到这一点。

<template name="websites">
  <h2>My Websites list</h2>
  <div class="row">
    {{#each websites}}
      Index:{{@index}}
      Feild1:{{feild1}}
      Feild2:{{feild1}}
      ...
    {{/each}}
  </div>
</template>
Template.websites.helpers({
  websites: function () {
    return Websites.find({_id: {$lte: website_id}}, {sort:{upvotes: -1}});
  },
  index: function (index) {
    return ++index;
  },
})

我没有创建任何表或列表,但根据您的要求,您也可以添加它。我希望干草能解决问题。

Websites.find({_id: {$lte: website_id}}, {sort:{upvotes: -1}}).fetch()