Is there a way of appending a point to a line with RethinkDB

52 views Asked by At

Assuming I am creating a geospatial line object with the same syntax as described in the documentation

r.table('geo').insert({
    id: 101,
    route: r.line([-122.423246,37.779388], [-121.886420,37.329898])
}).run(conn, callback);

Is there a way of appending a point to this line without using an atomic (read/write) pattern

1

There are 1 answers

2
Jorge Silva On BEST ANSWER

If you already inserted the document into the database and want to add a point to that document, you can use the udpate command with the toGeojson command in order to do that.

Here's how that would look:

r.table('30711279').get(101)
  .update(function (row)  {
    return {
      'route': r.line(r.args(
        row('route').toGeojson()('coordinates')
            .append([ -120, 85 ])
      ))
    }
  }, {nonAtomic: true })

Basically, I'm creating a new line in the route property. I'm passing it all the old points as an array and then appending a new tuple with the longitude and latitude.

If you still haven't inserted the document into the database and wanted to add a point to that line, you could just do this:

var arrayOfPoints = [[-122.423246,37.779388], [-121.886420,37.329898]];
var point = [ -120, 57 ];

r.table('geo').insert({
    id: 101,
    route: r.line(r.args(arrayOfPoints.concat(point)))
}).run(conn, callback);