简体   繁体   English

Meteor.js / MongoDB:在集合中查找文档的位置

[英]Meteor.js / MongoDB : finding a document's place in a collection

Solved thanks to Hubert OG! 解决了,感谢Hubert OG!

I have a collection with a popularity property and i do a: 我有一个具有人气属性的收藏,我这样做:

var x = collection.find({},{sort: {popularity: 1}})

and then i want to find the position (index) of a document (using it's id) by this particular sorting. 然后我想通过这种特殊的排序来查找文档的位置(索引)(使用其ID)。

How can i do that? 我怎样才能做到这一点? Do i have to convert the cursor to an array and loop through it or is there anything built into meteor that can give me an index? 我是否必须将游标转换为数组并循环遍历,还是流星内置了可以给我索引的任何内容?

Thank you so much in advance, Daniel. 非常感谢您,丹尼尔。

You can find out how many documents have larger popularity: 您可以发现有多少个文档具有较高的知名度:

var popularity = Documents.findOne(documentId).popularity;

var morePopular = Documents.find({popularity: {$gt: popularity}}).count();

If i understood correctly :) 如果我正确理解:)

var first = collection.find({}).fetch();

var x = collection.find({},{sort: {popularity: 1}}).fetch();

for(var m=0; m<x.length;m++){
    console.log(first.indexOf(x[m].fieldName))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM