0

I am trying to run a query that allows me to filter for a specific document, and then check the distance between the coordinates stored there, and the new ones I'm passing in.

I've tried this:

r.db('food').table('fruits')
  .hasFields(['origin', 'region'])
  .filter({region: 'North America'})
  .pluck('gpsLocation')
  .distance(r.point(37.759056, 105.015018))

but I get this error: e: Expected type DATUM but found SEQUENCE:

From the docs I see that I need

geometry.distance(geometry[, {geoSystem: 'WGS84', unit: 'm'}])

but I'm not sure how to get my query to return that. gpsLocation is an index on the fruits table if that makes a difference.

1 Answers1

0

You cannot pluck an index. I think you mean field, because the way you want to extract the data of that field with pluck. And you create an index with same name, from that field. If my assumption is correct, below is my answer. If not, you need to update your question because you cannot pluck an index.

The problem is wrong data type. According to https://rethinkdb.com/api/javascript/distance/, command syntax looks liek this:

geometry.distance(geometry[, {geoSystem: 'WGS84', unit: 'm'}]) → number
r.distance(geometry, geometry[, {geoSystem: 'WGS84', unit: 'm'}]) → number

That means distance can be either call on r, passing two geometyr object, or calling on a geometry object, and passing another geometry object as its first parameter.

Your query is returning a STREAM. You can found out its data type via reading document of API, or just use typeOf.

r.db('food').table('fruits')
  .hasFields(['origin', 'region'])
  .filter({region: 'North America'})
  .pluck('gpsLocation')
  .typeOf()

So, you have to somehow loop over the STREAM, and calling distance on a document of stream.

r.db('food').table('fruits')
  .hasFields(['origin', 'region'])
  .filter({region: 'North America'})
  .pluck('gpsLocation')
  .map(function(location) {
    return location.distance(r.point(37.759056, 105.015018))
  })

It's a bit similar to how you have an array, and calling map in JavaScript to walk over the array, and running a callback function on the element. Though here, the map function runs on server.

With assume that you gpsLocation field contains an geometry object, such as a point(https://rethinkdb.com/api/javascript/point/) with longitude and latitude.

kureikain
  • 2,304
  • 2
  • 14
  • 9
  • The mapping is what I needed to do in order to get it to work, thanks. I don't quite understand what you mean by you can't `pluck` on an `index`. I have `gpsLocation` as a secondary index, and when I use `pluck` it does return me the geometry object stored there. That's why I was trying to chain .distance() onto that. Perhaps I used the `index` terminology wrong. – wildrhubarb Oct 01 '15 at 17:30
  • I think I understand your confusing. You *do* have a field name `gpsLocation`. Then you create a secondary index from that field, hence the index shares same name `gpsLocation`. That's why you can pluck it. Because pluck accept a list of field to,well, pluck from document. If you create an index, where as the name of index doesn't match any field. Calling pluck on that name will return empty document `{}`. If the answer helps you, please accept it. – kureikain Oct 01 '15 at 17:48