如何在 RethinkDB 中对 getNearest() 查询运行过滤器? [英] How do I run a filter on a getNearest() query in RethinkDB?

查看:55
本文介绍了如何在 RethinkDB 中对 getNearest() 查询运行过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中包含用户及其位置保存为 r.point 数据类型 &对它们设置的 geo 索引.我正在尝试运行一个 .getNearest() 查询,它返回最接近给定用户的所有用户(例如,X 先生).结果返回所有最接近 Mr. 的用户.X,但也包括 Mr.X.如何过滤所有用户除了 Mr.X?

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?

到目前为止我所尝试的 —

  1. 在 RethinkDB 的数据浏览器中(普通 ReQL 命令)

  1. 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`

这将返回所有用户,包括 Mr.X.尝试 #2 是 -

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'));

这不会返回任何内容.

thinky 中,RethinkDB 的 Node.js ORM.

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();

这将返回所有用户,包括 Mr.X.

This returns all users, including Mr. X.

我已经知道的一种解决方案 —

使用 Thinky,我可以做到这一点.

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

我觉得我可以做得更快;通过确保 .filter() 或其他替代函数在 RethinkDB 服务器上运行而不是在我的服务器上运行.这里有改进的余地吗?或者也许是我忽略的更好的解决方案?

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?

推荐答案

你实际上做对了,几乎就到了.你只是碰巧用错误的键使用了 filter.

You actually do it correct and almost there. You just happen to use filter with wrong key.

r.db('myDb').table('myTable').getNearest(r.point(-20, 39), {index: 'location'})
    .filter(function(user) {
           return user('doc')('id').ne('2ff8902e-97f0-431a-a51c-900a57532967')
    })

瘦的关键是:

  1. getNearest 返回一个文档数组.每个文档有两个字段:distdoc(这是表格本身中的文档).因此,这里我们使用 user('doc')('id') 来获取密钥.https://rethinkdb.com/api/javascript/get_nearest/
  2. filter 函数中,我们必须使用ReQL 命令,在这种情况下,ne 的意思是,不相等.https://www.rethinkdb.com/api/javascript/ne/立>
  1. getNearest returns an array of document. Each document has two fields: dist and doc(which is the document in table itself). Hence here we are using user('doc')('id') to get the key. https://rethinkdb.com/api/javascript/get_nearest/
  2. Inside filter function, we have to use ReQL command, in this case, the ne mean, not equal. https://www.rethinkdb.com/api/javascript/ne/

这很快,因为我们有位置索引.而 filter 函数在 RethinkDB 服务器上执行.

This is fast because we have the index on location. And filter function is executed on RethinkDB server.

这篇关于如何在 RethinkDB 中对 getNearest() 查询运行过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆