- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想发送一个带有地址的 http 帖子,它将从数据库中获取一个文档。然后我想在 getNearest 函数中使用该文档的地理数据,最终返回最近的四个其他位置。我该如何将这两个查询串在一起?
r.table('dealer_locations').filter(function(dealer) {
return {
dealer : ("Address").match(request.body.Address)
.getNearest(dealer, {
index: 'geodata',
maxResults: 4
})
}
}).run(conn, function(error, cursor) {
if (error) {
handleError(response, error);
} else {
cursor.toArray(function(error, results) {
if (error) {
handleError(response, error);
} else {
response.send(results);
}
});
}
});
最佳答案
我将重新表述问题以使其更清楚:
问题
给定包含地理数据的特定文档,我还想返回距该位置最近的四个位置。
解决方案
首先,确保您已在要查询的表中创建地理索引:
r.table('dealer_locations').indexCreate('location', { geo: true })
之后,确保您已插入 r.point
对象到文档中。您不能只对任何属性(property)使用地理索引。它们必须是 r.point
。
r.table('dealer_locations')
.insert({
name: 'San Francisco',
location: r.point(37.774929, -122.419416)
})
插入所有文档后,它们都有 r.point
s 属性位于您为其创建索引的同一属性上,现在您可以开始查询它们了。
如果你想得到一个位置的所有最近位置,你可以这样做:
r.table('dealer_locations')
.filter({ name: 'San Francisco' })(0)
.do(function (row) {
return r.table('dealer_locations')
.getNearest(row('location'), { index: 'location', maxResults: 4 })
})
如果您想将壁橱位置附加到文档中,以便您可以同时返回文档和最近的位置,您可以使用 merge
来实现。方法。
r.table('dealer_locations')
.filter({ name: 'San Francisco' })(0)
.merge(function (row) {
return {
nearest_locations: r.table('dealer_locations')
.getNearest(row('location'), { index: 'location', maxResults: 4 })
}
})
最后,如果您想根据地址获取所有最近的位置(假设您的文档同时具有带字符串的 address
属性和带字符串的 location
属性r.point
),你可以这样做:
r.table('dealer_locations')
.filter(r.row('address').match(request.body.Address))
(0) // Only get the first document
.do(function (row) {
// Return the 4 documents closest to the 'location' if the previous document
return r.table('dealer_locations')
.getNearest(row('location'), { index: 'location', maxResults: 4 })
})
也就是说,这样做的问题可能是您可能会匹配多个地址,而这些地址不一定是您想要匹配的地址!
关于javascript - 在另一个中使用一个 RethinkDB 查询的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194756/
RethinkDb 是否支持类似于 Mysql 中的 AUTO_INCREMENT 的整数列上的 AUTO_INCREMENT。 https://dev.mysql.com/doc/refman/5.
我可以在 RethinkDB 中创建复合主键吗?例如,如果我有下一个文档结构的表格 {authorsId: '371df80c-2efd-48f9-ac37-3edcdb152fb1', postsI
假设我有一个“类别”表,每个类别在“数据”表中都有关联数据,并且在其他“关联”表中都有关联数据,并且我想删除一个类别及其所有关联数据。我目前正在做的事情是这样的: getAllDataIdsFromC
我一直在寻找有关写入原子性的信息,但没有找到有关读取的信息。读取总是原子的吗?例如,我传递一个只读查询,通过查询多个不同的文档来使用append()填充数组。基本上,在查询开始出现在结果中后,我是否能
我正在尝试根据哈希中的一些预先计算的值更新 RethinkDB 中的多个文档。即 给定一个带有主键 slug 的表 stats 以及诸如 的数据 [{slug: 'foo', stats: {}},
RethinkDb 是否支持创建/管理数千个表? 用例:租户管理的表,没有已知架构。 编辑 future 引用:https://github.com/rethinkdb/rethinkdb/issue
我不知道如何获取“test.events”表的数据大小。 r.db('rethinkdb').table('stats').whatGoesHere() // Output size of 'even
我正在使用带有官方 python 驱动程序的 Rethinkdb 1.10.1。我有一个与一个用户关联的标记事物表: { "id": "PK", "user_id": "USER_PK
我需要从命令行以编程方式创建 RethinkDB 数据库。但是......我不知道如何做到这一点。 我知道我可以从 Web UI 或客户端驱动程序执行此操作,但是没有执行此操作的命令吗? 最佳答案 正
给定的样本数据,如: { 'id': 1, 'things': [{'name': 'a'},{'name': 'b'},{'name': 'c'}] } 如何更新文档以从嵌入的数组中删除
我正在处理一个看起来像这样的数据集: "bitrates": [ { "format": "mp3" , "rate": "128K" } , { "forma
我有 2 个 RethinkDB 表: Left: { id: String, title: String, key: String // for mapping with table R
如果数组存在,我们应该如何将一个项目追加到数组中,或者创建一个数组并插入到其中。 我尝试了合并命令,但不允许合并数组,只能替换它们。 r.db('testdb').table('users').get
我对 RethinkDB 中的连接如何工作有点模糊。每次执行查询时,我都会打开一个新连接,而不会在查询完成后关闭它们。 这是一个好的做法吗?或者我应该在查询完成后显式关闭连接? (我正在使用 JS 驱
我想限制 Rethinkdb 查询中嵌套对象的数量。假设我有嵌套消息的对话。 [对话] [{ id: "fgh675", name: "Some conversation",
我正在尝试从 RethinkDB 文档中删除 key 。我的方法(不起作用): r.db('db').table('user').replace(function(row){delete row["k
我正在实现一个使用 RethinkDB 来存储数据的应用程序(HTML 和 AngularJS)。 基本上,任何人都可以从 .js 文件获取 RethinkDB 连接凭据。 那么如何才能让用户只获取自
测试 RethinkDB 并想知道使用 JavaScript 客户端插入 1m 行的正确方法。目前正在执行类似以下操作,但要么简单地停止插入而没有错误,要么出现内存异常: fo
有没有办法在 rethinkdb 中取消订阅或更改现有的 changefeed 观察者?将 changes() 函数的返回值设置为 null 似乎没有任何作用,是否有 unsubscribe() 函数
我想在 RethinkDB 中随机排序文档。这样做的原因是我返回了n组文档,并且每组必须在结果中按顺序出现(所以属于一个组的所有文档应该放在一起);我需要随机选择一个文档,属于结果中的第一组(您不知道
我是一名优秀的程序员,十分优秀!