Search code examples
c#rethinkdbreql

How to update or insert an item in RethinkDB?


I am trying to update or insert (if it does not exist) an element in the database but so far I have not found any examples on how to use the Update and Filter provided by the C# Api.(C# Api , not RethinkDB Reql).

Link to api : https://github.com/bchavez/RethinkDb.Driver

Can anyone help me ?

POCO

 class Player
    {

        public long playerId { get; set; }
        public long groupId { get; set; }
        public int type { get; set; }


        public double lat { get; set; }
        public double lng { get; set; }
    }

class PlayerRequest
    {

        public  Player data { get; set; }
        public  Auxdata aux { get; set; }
    }

This would be the insert request:

dynamic rez = await r.Db(Constants.DB_NAME).Table(Constants.TABLE_CLIENT)
              .Insert(playerRequest.data).RunAsync(Con);

I need to perform an update of playerRequest.data based on the playerId or insert the Player if it does not exist.

I have searched the Api but so far no success.

P.S I have tried installing the Linq to ReQL extension, but the methods seam to be unavailable.


Solution

  • After searching and asking the author of the driver which helped me greatly (thank you Brian!) the solution is the following:

    In Js we define adhoc objects with {prop1:value1,prop2,value2}

    The C# equivalent is using anonymous types: new{prop1:value1,prop2:value2}

    r.Db(Constants.DB_NAME).Table(Constants.CLIENT_TABLE)
                            .Filter(new { playerId = playerRequest.data.playerId })
                            .Update(new { lng = playerRequest.data.lng, lat = playerRequest.data.lat })
                            .RunResult<QueryResult>(this.Con);
    

    Also for further explanation you must read this section of the github repository wiki: https://github.com/bchavez/RethinkDb.Driver/wiki/Extra-C%23-Driver-Features

    When in doubt on how to use a method check the Java driver implementation,they are similar (The C# driver extends the Java one)