I want to send a http post with an address, which will grab a doc from the database. I would like to then use that doc's geodata in a getNearest function, ultimately returning the nearest four other locations. How do I go about stringing these two queries together?
我想发送一个带有地址的http post,它将从数据库中获取一个doc。然后我想在getNearest函数中使用doc的地理数据,最终返回最近的四个位置。如何将这两个查询串在一起?
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);
}
});
}
});
1 个解决方案
#1
3
I'll reformulate the question just to make a bit more clear:
我将重新表述这个问题,让大家更清楚一点:
Problem
问题
Given a specific document with geodata, I want to also return the four nearest locations to that location.
给定一个带有地理数据的特定文档,我还想返回到该位置的四个最近位置。
Solution
解决方案
First, make sure that you've creating the geo index in the table you want to query:
首先,确保在要查询的表中创建了geo索引:
r.table('dealer_locations').indexCreate('location', { geo: true })
After that, make sure that you've inserted a r.point
object into the document. You can't just use geo indexes on just any property. They have to be r.point
s.
然后,确保插入了一个r。将对象指向文档。不能只在任何属性上使用geo索引。它们必须是r点。
r.table('dealer_locations')
.insert({
name: 'San Francisco',
location: r.point(37.774929, -122.419416)
})
After you've inserted all your documents and they all have r.point
s property on the same property you created an index for, now you can start querying them.
插入所有文档后,它们都有r。在创建索引的相同属性上的points属性,现在可以开始查询它们。
If you want to get all the nearest locations for a location, you can do as follows:
如果您想获取一个位置的所有最近的位置,您可以做以下工作:
r.table('dealer_locations')
.filter({ name: 'San Francisco' })(0)
.do(function (row) {
return r.table('dealer_locations')
.getNearest(row('location'), { index: 'location', maxResults: 4 })
})
If you want to append the closets locations to a document, so that you can return both the document and the nearest locations at the same time, you can do that using the merge
method.
如果希望将closet位置附加到文档中,以便可以同时返回文档和最近的位置,那么可以使用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 })
}
})
Finally, if you want to get all the nearest locations, based on an address (supposing your document has both an address
property with a string and a location
property with an r.point
), you can do something like this:
最后,如果您想根据一个地址获得所有最近的位置(假设您的文档有一个带字符串的地址属性和一个带r点的位置属性),您可以这样做:
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 })
})
That being said, the problem with this might be that you might match multiple addresses which are not necessarily the ones you want to match!
话虽如此,问题可能在于您可能匹配多个地址,而这些地址不一定是您想匹配的地址!
#1
3
I'll reformulate the question just to make a bit more clear:
我将重新表述这个问题,让大家更清楚一点:
Problem
问题
Given a specific document with geodata, I want to also return the four nearest locations to that location.
给定一个带有地理数据的特定文档,我还想返回到该位置的四个最近位置。
Solution
解决方案
First, make sure that you've creating the geo index in the table you want to query:
首先,确保在要查询的表中创建了geo索引:
r.table('dealer_locations').indexCreate('location', { geo: true })
After that, make sure that you've inserted a r.point
object into the document. You can't just use geo indexes on just any property. They have to be r.point
s.
然后,确保插入了一个r。将对象指向文档。不能只在任何属性上使用geo索引。它们必须是r点。
r.table('dealer_locations')
.insert({
name: 'San Francisco',
location: r.point(37.774929, -122.419416)
})
After you've inserted all your documents and they all have r.point
s property on the same property you created an index for, now you can start querying them.
插入所有文档后,它们都有r。在创建索引的相同属性上的points属性,现在可以开始查询它们。
If you want to get all the nearest locations for a location, you can do as follows:
如果您想获取一个位置的所有最近的位置,您可以做以下工作:
r.table('dealer_locations')
.filter({ name: 'San Francisco' })(0)
.do(function (row) {
return r.table('dealer_locations')
.getNearest(row('location'), { index: 'location', maxResults: 4 })
})
If you want to append the closets locations to a document, so that you can return both the document and the nearest locations at the same time, you can do that using the merge
method.
如果希望将closet位置附加到文档中,以便可以同时返回文档和最近的位置,那么可以使用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 })
}
})
Finally, if you want to get all the nearest locations, based on an address (supposing your document has both an address
property with a string and a location
property with an r.point
), you can do something like this:
最后,如果您想根据一个地址获得所有最近的位置(假设您的文档有一个带字符串的地址属性和一个带r点的位置属性),您可以这样做:
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 })
})
That being said, the problem with this might be that you might match multiple addresses which are not necessarily the ones you want to match!
话虽如此,问题可能在于您可能匹配多个地址,而这些地址不一定是您想匹配的地址!