Reputation: 21279
I've a collection of addresses, I would like to filter the collection to keep the 10 nearest address, then I would like to be able to sort them from the farther to the nearest.
Is that possible to achieve this within a single find request in meteor ?
The following gives me the 10 nearest addresses:
Addresses.find({}, {sort:{distance:1}, limit:10})
;
but they are ordered by increasing distance, obviously if I do set distance:-1
they will come by decreasing order but I will also get only the 10 farthest addresses…
Upvotes: 1
Views: 630
Reputation: 6509
If you fetch
the result of your search and reverse
it it should work.
Addresses.find({}, {sort:{distance:1}, limit:10}).fetch().reverse()
The only drawback is that now it's an array and not a cursor anymore
Upvotes: 0
Reputation: 12240
You need the aggregation framework:
db.collection.aggregate(
{ $sort: { distance: 1 } },
{ $limit: 10 },
{ $sort: { distance: -1 } }
)
I hope the query is self-explanatory.
If you can't run an aggregation
or native mongo query in MeteorJS
, then you'll probably have to reverse the results you got from the DB query programatically.
Upvotes: 1