user1720271
user1720271

Reputation: 57

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

Solved thanks to 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.

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.

Upvotes: 1

Views: 178

Answers (2)

Hubert OG
Hubert OG

Reputation: 19544

You can find out how many documents have larger popularity:

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

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

Upvotes: 2

delibalta
delibalta

Reputation: 298

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))
}

Upvotes: 0

Related Questions