I have a table with users and their locations saved as r.point
datatypes & a geo
index set on them. I am trying to run a .getNearest()
query, which returns all the users closest to the given user (say, Mr. X). The results return all the users closest to Mr. X, but also include Mr. X. How do I filter all users except Mr. X?
What I've tried so far —
In RethinkDB's Data Explorer (Plain ReQL commands)
r.db('myDb').table('myTable').getNearest(r.point(-20, 39), {index: 'location'}).filter(function(user) {return user.id !== '2ff8902e-97f0-431a-a51c-900a57532967'}); // Assuming Mr.X's `id` is `2ff8902e-97f0-431a-a51c-900a57532967`
This returns all users, including Mr. X. Attempt #2 was -
r.db('myDb').table('myTable').getNearest(r.point(-20, 39), {index: 'location', maxDist:9000000}).filter(r.row('id').ne('2ff8902e-97f0-431a-a51c-900a57532967'));
This returns nothing.
In thinky, a Node.js ORM for RethinkDB.
UserModel.getNearest(r.point(-20, 39), {index: 'location'}).filter(function(user) {return user.id !== '2ff8902e-97f0-431a-a51c-900a57532967'}).run();
This returns all users, including Mr. X.
One solution I'm already aware of —
Using Thinky, I can do this.
const userId = '2ff8902e-97f0-431a-a51c-900a57532967';
const location = r.point(-20, 39);
const queryOptions = {index: 'location'};
UserModel.getNearest(location, queryOptions).run().then(function (users) {
return users.filter(function(user) {
return user.doc.id !== userId;
});
});
I have a feeling I can make this faster; by making sure either the .filter()
or another alternative function is run on the RethinkDB server than on my end. Is there room for improvement here? Or perhaps a nicer solution that I am overlooking?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…