在另一个中使用一个 RethinkDB 查询的结果?
Using results from one RethinkDB query in another?
我想发送一个带有地址的 http post,它将从数据库中获取文档。然后我想在 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);
}
});
}
});
我将重新表述问题,以便更清楚一点:
问题
给定包含地理数据的特定文档,我还想 return 离该位置最近的四个位置。
解决方案
首先,确保您已在要查询的 table 中创建地理索引:
r.table('dealer_locations').indexCreate('location', { geo: true })
之后,确保您已将 r.point
对象插入到文档中。您不能只在任何 属性 上使用地理索引。他们必须是 r.point
s.
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 })
})
如果您想将壁橱位置附加到文档中,以便您可以同时 return 文档和最近的位置,您可以使用 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 })
})
也就是说,这个问题可能是您可能会匹配多个地址,而这些地址不一定是您想要匹配的地址!
我想发送一个带有地址的 http post,它将从数据库中获取文档。然后我想在 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);
}
});
}
});
我将重新表述问题,以便更清楚一点:
问题
给定包含地理数据的特定文档,我还想 return 离该位置最近的四个位置。
解决方案
首先,确保您已在要查询的 table 中创建地理索引:
r.table('dealer_locations').indexCreate('location', { geo: true })
之后,确保您已将 r.point
对象插入到文档中。您不能只在任何 属性 上使用地理索引。他们必须是 r.point
s.
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 })
})
如果您想将壁橱位置附加到文档中,以便您可以同时 return 文档和最近的位置,您可以使用 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 })
})
也就是说,这个问题可能是您可能会匹配多个地址,而这些地址不一定是您想要匹配的地址!