gpt4 book ai didi

javascript - 在另一个中使用一个 RethinkDB 查询的结果?

转载 作者:搜寻专家 更新时间:2023-11-01 00:42:23 25 4
gpt4 key购买 nike

我想发送一个带有地址的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com