0

I'm fairly new to RethinkDB and am trying to solve a thorny problem.

I have a database that currently consists of two kinds of account, customers and technicians. I want to write a query that will produce a table of technicians, ordered by their distance to a given customer. The technician and customer accounts each have coordinate location attributes, and the technicians have a service area attribute in the form of a roughly circular polygon of coordinates.

For example, a query that returns a table of technicians whose service area overlaps the location of a given customer looks like this:

r.db('database').table('Account')
.filter(r.row('location')('coverage').intersects(r.db('database')
.table('Account').get("6aab8bbc-a49f-4a9d-80cc-88c95d0bae8d")
.getField('location').getField('point')))

From here I want to order the resulting subtable of technicians by their distance to the customer they're overlapping.

oleson
  • 581
  • 4
  • 3

1 Answers1

1

It's hard to work on this without a sample dataset so I can play around. I'm using my imagination.

  • Your Account table stores both of customer and technician
  • Technician document has field location.coverage

By using intersect, you can returns a list of technician who the coverage locations includes customer location.

To order it, we can pass a function into orderBy command. With each of technican, we get their point field using distance command, return that distance number, and using that to order.

r.db('database').table('Account')
.filter(
  r.row('location')('coverage')
    .intersects(
    r.db('database').table('Account').get("6aab8bbc-a49f-4a9d-80cc-88c95d0bae8d")('location')('point')
  )
)
.orderBy(function(technician) {
    return technician('location')('point')
            .distance(r.db('database').table('Account').get("6aab8bbc-a49f-4a9d-80cc-88c95d0bae8d")('location')('point'))
})

I hope it helps. If not, let's post some sample data here and we can try figure it out together.

kureikain
  • 2,304
  • 2
  • 14
  • 9