0

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 Answers1

1

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);
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42
  • Thanks, the update method is what I was looking for, guess i cant avoid loading the entire route into memory before appending though – Patt-tom McDonnell Jun 08 '15 at 19:13
  • No, you can't. But keep in mind that this is all happening in the database, not in your application. That's much faster than doing it in your application logic. – Jorge Silva Jun 08 '15 at 19:20
  • If this answered your question, please make sure to mark it as answered! :) Thanks – Jorge Silva Jun 08 '15 at 19:21