[英]Get position of object after sort in MongoDB
我正在開發一個Web應用程序,並且正在使用MongoDB來存儲數據。 我能夠對數據進行排序並正確顯示它,但是我想在使用Mongo對數據進行排序后獲得對象的位置。 到目前為止,我有以下代碼:
Websites.find({_id: {$lte: website_id}}, {sort:{upvotes: -1}}).count();
它不太起作用。 它返回位置的排序,但與sort:{upvotes: -1}
不相關。
因此,問題是,如何才能將1
返回最高出價,將2
返回第二,依此類推?
Websites.find({_id: {$lte: website_id}}, {sort:{upvotes: -1}}).fetch()
這就是您可以做到的。
<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;
},
})
我沒有創建任何表或列表,但根據您的要求,您也可以添加它。 我希望干草能解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.